embedded python and threading

Stefan Behnel stefan_ml at behnel.de
Fri Jul 26 11:29:49 CEST 2013

David M. Cotter, 26.07.2013 08:15:
> 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?

You didn't show your code, but as a wild guess, maybe you did

    from time import time

instead of

    import time

somewhere? If not, please provide the exact example code that you are running.


More information about the Python-list mailing list