I have a tiny Python script which (in my eyes) makes threading.Thread.start()
behave unexpectedly since it does not return immediately.
Inside a thread I want to call a method from a boost::python
based object which will not return immediately.
To do so I wrap the object/method like this:
import threading
import time
import my_boostpython_lib
my_cpp_object = my_boostpython_lib.my_cpp_class()
def some_fn():
# has to be here - otherwise .start() does not return
# time.sleep(1)
my_cpp_object.non_terminating_fn() # blocks
print("%x: 1" % threading.get_ident())
threading.Thread(target=some_fn).start()
print("%x: 2" % threading.get_ident()) # will not always be called!!
And everything works fine as long as I run some code before my_cpp_object.non_terminating_fn()
. If I don't, .start()
will block the same way as calling .run()
directly would.
Printing just a line before calling the boost::python
function is not enough, but e.g. printing two lines or calling time.sleep()
makes start()
return immediately as expected.
Can you explain this behavior? How would I avoid this (apart from calling sleep()
before calling a boost::python
function)?