quote:
It's not so much that they're not performed, it's that because you called a reactor API from a thread, which you are never ever ever allowed to do , you have put your program into a state where everything is completely broken, forever . Every future API call may produce bizarre, broken results, or no results, or random, inexplicable crashes.
You know, the normal way multithreaded programs work ;-).
To repeat: every API in twisted, with the sole exception of callFromThread (and by extension things which call callFromThread , like blockingCallFromThread ), is not thread safe . Unfortunately, putting in warnings for every single API would be both a code maintenance nightmare, so several users have discovered this constraint in the same way that you have, by calling an API and noticing something weird.
If you have some code which runs in a thread that needs to call a reactor API, use callFromThread or blockingCallFromThread and it will dispatch the call to the reactor thread, where everything should work smoothly. However, for stuff like timed calls, there's really no need to use threads at all, and they would needlessly complicate your program.
by Glyph
link: http://stackoverflow.com/questions/3220991/difference-between-loopingcall-and-callinthread-in-pythons-twisted
本文详细解释了在Python的Twisted框架中正确进行多线程调用的方法。强调了除了callFromThread和blockingCallFromThread外,所有API均不是线程安全的,并介绍了如何使用这些安全API来避免程序状态混乱。
479

被折叠的 条评论
为什么被折叠?



