Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 240747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:41:33+00:00 2026-05-11T20:41:33+00:00

Certain functions in my code take a long time to return. I don’t need

  • 0

Certain functions in my code take a long time to return. I don’t need the return value and I’d like to execute the next lines of code in the script before the slow function returns. More precisely, the functions send out commands via USB to another system (via a C++ library with SWIG) and once the other system has completed the task, it returns an “OK” value. I have reproduced the problem in the following example. How can I make “tic” and “toc” print one after the other without any delay? I suppose the solution involves threads, but I am not too familiar with them. Can anyone show me a simple way to solve this problem?

from math import sqrt
from time import sleep

def longcalc():
    total = 1e6
    for i in range(total):
        r = sqrt(i)
    return r

def longtime():
    #Do stuff here
    sleep(1)
    return "sleep done"

print "tic"
longcalc()
print "toc"
longtime()
print "tic"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:41:33+00:00Added an answer on May 11, 2026 at 8:41 pm

    Unless the SWIGged C++ code is specifically set up to release the GIL (Global Interpreter Lock) before long delays and re-acquire it before getting back to Python, multi-threading might not prove very useful in practice. You could try multiprocessing instead:

    from multiprocessing import Process
    
    if __name__ == '__main__':
        print "tic"
        Process(target=longcalc).start()
        print "toc"
        Process(target=longtime).start()
        print "tic"
    

    multiprocessing is in the standard library in Python 2.6 and later, but can be separately downloaded and installed for versions 2.5 and 2.4.

    Edit: the asker is of course trying to do something more complicated than this, and in a comment explains:
    “””I get a bunch of errors ending with: "pickle.PicklingError: Can't pickle <type 'PySwigObject'>: it's not found as __builtin__.PySwigObject". Can this be solved without reorganizing all my code? Process was called from inside a method bound to a button to my wxPython interface.”””

    multiprocessing does need to pickle objects to cross process boundaries; not sure what SWIGged object exactly is involved here, but, unless you can find a way to serialize and deserialize it, and register that with the copy_reg module, you need to avoid passing it across the boundary (make SWIGged objects owned and used by a single process, don’t have them as module-global objects particularly in __main__, communicate among processes with Queue.Queue through objects that don’t contain SWIGged objects, etc).

    The early errors (if different than the one you report “ending with”) might actually be more significant, but I can’t guess without seeing them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 111k
  • Answers 111k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try something like this: NSLog(@"Object: %@", object); NSLog(@"Color: %@", [object… May 11, 2026 at 9:44 pm
  • Editorial Team
    Editorial Team added an answer The SmtpClient class represents (as the name suggests) an smtp… May 11, 2026 at 9:44 pm
  • Editorial Team
    Editorial Team added an answer The KnowledgeSyncProvider class seems to have been in the Microsoft… May 11, 2026 at 9:44 pm

Related Questions

I'm currently refactoring a console application whose main responsibility is to generate a report
I am currently doing some Project Euler problems and the earlier ones often involve
Let me just preface by saying it's actually my crappy code that's leaking and
I'm looking to develop a Silverlight application which will take a stream of data
I am performing a jQuery .ajax() call that returns a List<string> of IP addresses

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.