embedded python and threading

David M. Cotter me at davecotter.com
Fri Jul 26 08:15:28 CEST 2013


in my app i initialize python on the main thread, then immediately call PyEval_SaveThread() because i do no further python stuff on the main thread.

then, for each script i want to run, i use boost::threads to create a new thread, then on that thread i "ensure" the GIL, do my stuff, then release it.

so, to test concurrency, on my first background thread, i do an infinite loop that just logs "i'm alive", then calls sleep(0.25)

so that thread continues to run forever (with it's GIL ensured)

according to the doc: "In order to emulate concurrency of execution, the interpreter regularly tries to switch threads"

so i figure i can run another thread that does a single print statement:

> ensure gil
> print my thing
> release gil

and this DOES run.  however, after releasing it's gil, i guess the interpeter gets back to the first back thread, but then has this error immediately:

    9: Traceback (most recent call last):
    9:   File "<string>", line 70, in ?
    9:   File "<string>", line 55, in main
    9: AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'

suddenly the sleep module has been unloaded?? huh?  i thought the thread state had been preserved?



More information about the Python-list mailing list