Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69714
| References | <BLU402-EAS3761BB1DD75580AE06D38FA9D6E0@phx.gbl> |
|---|---|
| Date | 2014-04-05 16:22 +1100 |
| Subject | Re: About threading.Thread |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8919.1396675349.18130.python-list@python.org> (permalink) |
On Sat, Apr 5, 2014 at 4:02 PM, 张佩佩 <zhangpeipei812@outlook.com> wrote: > def fun(): > a = threading.Thread(target=hello(), name='hello') > It seems that threading.Thread() in file1 not create a new thread but use MainThread. > Anyone can explain this ? > Thank you in advance. Suggestion: Cut the code down until you find the exact bit that's showing a problem. You don't need two files for this; in fact, all you need is your definition of hello, the call to threading.Thread(), and a print statement after it, which you'll see doesn't happen. The problem here is that you're already *calling* hello() in the argument list. Before threading.Thread() gets called, its arguments get fully evaluated... which calls hello(), which infinitely loops. That's why it's getting called on the main thread. To spin off a thread that will call hello(), take the parentheses off: a = threading.Thread(target=hello, name='hello') That'll pass a function, rather than the return value of that function, and then Thread can call that. ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: About threading.Thread Chris Angelico <rosuav@gmail.com> - 2014-04-05 16:22 +1100
csiph-web