Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51286 > unrolled thread
| Started by | "David M. Cotter" <me@davecotter.com> |
|---|---|
| First post | 2013-07-25 23:15 -0700 |
| Last post | 2013-07-26 20:38 +0200 |
| Articles | 8 — 4 participants |
Back to article view | Back to comp.lang.python
embedded python and threading "David M. Cotter" <me@davecotter.com> - 2013-07-25 23:15 -0700
Re: embedded python and threading Stefan Behnel <stefan_ml@behnel.de> - 2013-07-26 11:29 +0200
Re: embedded python and threading "David M. Cotter" <me@davecotter.com> - 2013-07-26 08:57 -0700
Re: embedded python and threading David Robinow <drobinow@gmail.com> - 2013-07-26 12:14 -0400
Re: embedded python and threading John Gordon <gordon@panix.com> - 2013-07-26 16:34 +0000
Re: embedded python and threading "David M. Cotter" <me@davecotter.com> - 2013-07-26 10:25 -0700
Re: embedded python and threading "David M. Cotter" <me@davecotter.com> - 2013-07-26 10:28 -0700
Re: embedded python and threading Stefan Behnel <stefan_ml@behnel.de> - 2013-07-26 20:38 +0200
| From | "David M. Cotter" <me@davecotter.com> |
|---|---|
| Date | 2013-07-25 23:15 -0700 |
| Subject | embedded python and threading |
| Message-ID | <a6710206-8676-48c8-ad66-95139e695503@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2013-07-26 11:29 +0200 |
| Message-ID | <mailman.5138.1374831002.3114.python-list@python.org> |
| In reply to | #51286 |
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.
Stefan
[toc] | [prev] | [next] | [standalone]
| From | "David M. Cotter" <me@davecotter.com> |
|---|---|
| Date | 2013-07-26 08:57 -0700 |
| Message-ID | <965b463e-e5bf-4ccd-9a3c-b0cb964b3356@googlegroups.com> |
| In reply to | #51291 |
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'
==========================================
[toc] | [prev] | [next] | [standalone]
| From | David Robinow <drobinow@gmail.com> |
|---|---|
| Date | 2013-07-26 12:14 -0400 |
| Message-ID | <mailman.5153.1374855283.3114.python-list@python.org> |
| In reply to | #51313 |
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? On Fri, Jul 26, 2013 at 11:57 AM, David M. Cotter <me@davecotter.com> wrote: > 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' > ========================================== > -- > http://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-07-26 16:34 +0000 |
| Message-ID | <ksu8eu$oh0$1@reader1.panix.com> |
| In reply to | #51313 |
In <965b463e-e5bf-4ccd-9a3c-b0cb964b3356@googlegroups.com> "David M. Cotter" <me@davecotter.com> writes:
> ==========================================
> 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.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | "David M. Cotter" <me@davecotter.com> |
|---|---|
| Date | 2013-07-26 10:25 -0700 |
| Message-ID | <e7bf5211-bb6d-4379-bb1a-fba4e7007a31@googlegroups.com> |
| In reply to | #51315 |
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?
[toc] | [prev] | [next] | [standalone]
| From | "David M. Cotter" <me@davecotter.com> |
|---|---|
| Date | 2013-07-26 10:28 -0700 |
| Message-ID | <2c80d06f-6946-4624-ba3c-47ddfa9db117@googlegroups.com> |
| In reply to | #51316 |
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!
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2013-07-26 20:38 +0200 |
| Message-ID | <mailman.5155.1374863932.3114.python-list@python.org> |
| In reply to | #51318 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web