While experimenting with the Python Python API (python.org) C, I wondered how to correctly create threads through Python threadingwhen Python itself is embedded in a C program. Functions PyEval_EvalCodeand kin seem to complete the threads that it "owns" as soon as the C function completes the evaluation of the Python code block. For example, starting the next Python from C ...
import threading, time
class MyThread(threading.Thread):
def __init__(self, num):
self.num = num
threading.Thread.__init__(self)
def run(self):
print "magic num = %d" % self.num
for x in xrange(1000):
MyThread(x).start()
... will stop completely as soon as the for loop completes, and control returns from function PyEval_EvalCode(or such) C. We can observe that this gives a truncated output.
I suggested this behavior after using the following tactics: we can dictate when the control returns and, therefore, to some extent the result, by sleeping after spawning the spinning of threads:
for x in xrange(100):
MyThread(x).start()
time.sleep(5)
I suspect a possible approach is to create a new system thread dedicated to implementing and running Python. After spawning Python threads, Python code will sleep on a semaphore or something else until it is asked to close it. Then the question will be, how can I stream the thread to shut it down neatly? Similarly, the "main" block can simply join () for all threads; threads will then need to be signaled from C.
The solutions are greatly appreciated.
source
share