which one is thread safe?

本文详细解释了在Python的Twisted框架中正确进行多线程调用的方法。强调了除了callFromThread和blockingCallFromThread外,所有API均不是线程安全的,并介绍了如何使用这些安全API来避免程序状态混乱。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值