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"
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:
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.”””multiprocessingdoes 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 thecopy_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.