embedded python and threading

D

David M. Cotter

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?
 
S

Stefan Behnel

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:


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.

Stefan
 
D

David M. Cotter

okay, i have simplified it: here is the code

==========================================
import time

def main():
while True:
print "i'm alive"
time.sleep(0.25)

#---------------------------------
if __name__ == "__main__":
main()
==========================================

the new error is:

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

David Robinow

Works for me.
Except that if I then do:
touch time.py

I get the same error as you do.

Can you figure out the problem now?
 
J

John Gordon

In said:
==========================================
9: Traceback (most recent call last):
9: File "<string>", line 10, in ?
9: File "<string>", line 6, in main
9: AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'
==========================================

You must have a file named 'time.py' in the current directory, and the
import statement is getting that module instead of the system time module.
 
D

David M. Cotter

no, there is no "time.py" anywhere (except perhaps as the actual python library originally imported)

did you understand that the function works perfectly, looping as it should, up until the time i run a second script on a separate thread?
 
D

David M. Cotter

DOH! as my second thread, i had been using a sample script that i had copy-pasted without much looking at it. guess what? it prints the time. and yes, it did "from time import time", which explains it all.

thanks for the hints here, that helped me figure it out!
 
S

Stefan Behnel

David M. Cotter, 26.07.2013 19:28:
DOH! as my second thread, i had been using a sample script that i had copy-pasted without much looking at it. guess what? it prints the time. and yes, it did "from time import time", which explains it all.

Ah, and you were using the same globals dict for both scripts, I guess?
That explains it then.

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top