twisted 线程相关知识 callInThread(suggestThreadPoolSize),callFromThread

Twisted 的 callInThread 和 callFromThread 区别

这两个函数的定义在  IReactorThreads 的文档里。

 

Method callInThread

Run the callable object in a separate thread.

Method callFromThread

Cause a function to be executed by the reactor thread.

Use this method when you want to run a function in the reactor's thread from another thread. Calling callFromThread should wake up the main thread (where reactor.run() is executing) and run the given callable in that thread.

If you're writing a multi-threaded application the callable may need to be thread safe, but this method doesn't require it as such. If you want to call a function in the next mainloop iteration, but you're in the same thread, use callLater with a delay of 0.

 

也就是说,reactor.callFromThread 是在由 reactor.run() 激发的主消息循环(main event loop)中执行,所以也就能被 reactor.stop() 终止执行。甚至可以通过:

reactor.callFromThread(reactor.stop)

来主动要求主消息循环关闭 reactor 的主线程运行。

callFromThread 有时候比较危险,如果压的任务太多,会阻塞主消息循环,造成其他事件无法得到及时的处理。

 

参考 callInThread 的代码,可以看出它是在 reactor 的一个私有线程池里工作的:

def callInThread(self, _callable, *args, **kwargs):

    if self.threadpool is None:

        self._initThreadPool()

    self.threadpool.callInThread(_callable, *args, **kwargs)

所以,我们可以通过

from twisted.internet import reactor

reactor.suggestThreadPoolSize(15)

来设置该线程池的大小。默认最小是5个线程,最大10个(the default reactor uses a minimum size of 5 and a maximum size of 10)。

 

 

 

另外:

twisted里是通过回调机制来实现类似于多线程,意思是防止程序的运行由于等待某项任务的完成而陷入阻塞停滞,提高整体运行的效率。

from twisted.internet import reactor

1. reactor.callFromThread

Method callFromThread:

Cause a function to be executed by the reactor thread.

Use this method when you want to run a function in the reactor's thread from another thread. Calling callFromThread should wake up the main thread (where reactor.run() is executing) and run the given callable in that thread.

If you're writing a multi-threaded application the callable may need to be thread safe, but this method doesn't require it as such. If you want to call a function in the next mainloop iteration, but you're in the same thread, use callLater with a delay of 0.

此方法是本身是mainloop的线程,用reactor.stop()可以终止它。也可以reactor.callFromThread(reactor.stop)来终止它。但是这个方法会阻塞到主循环。

复制代码
#coding=utf-8
from twisted.internet import reactor
import time
def tt(i,j):
while1:
print i,'---------------',j
time.sleep(
1)

reactor.callFromThread(tt,
1,1)
reactor.callFromThread(tt,
4,2)
reactor.run()
复制代码

上面代码运行的结果是无限的打印1-------------------1,这个说明了主循环被阻塞住。

2. reactor.callInThread

Method callInThread:

Run the callable object in a separate thread.

此方法是创建独立的线程,用reactor stop方法无法停止它。这里涉及到一个线程池的概念

reactor.suggestThreadPoolSize(15)来设置线程池的大小,默认是最小是5,最大是10.如果在默认情况下

3. from twisted.internet import threads

threads.deferToThread(function),和callInThread一样,区别只是 deferToThread 可以返回一个deferred对象,从而允许你设定回调函数。

posted on 2012-05-15 11:26  很多不懂呀。。  阅读(3761)  评论(1编辑  收藏  举报

导航