2

我可以初始化一个 IOLoop.instance 并派生使用 IOLoop.instance() 的新进程吗?就像是:

#some code which initializes IOLoop.instance()           (1)
storage - Storage()
...
def f(storage):
    """some ioloop worker, which uses IOLoop.instance()"""
    storage.db_client.send(some value)
    ...

p1 = Process(target=f, args=(storage,))
p2 = Process(target=f, args=(storage,))

IOLoop文档没有说明多线程使用 IOLoop,但tornado.process.fork_processes 文档禁止在分叉之前初始化 IOLoop。

关键是 (1) 中的代码创建了storage由 function 使用的对象f。包含异步数据库客户端,storage假设使用与工作进程相同的 ioloop。

4

1 回答 1

4

询问tornado用户组后,我收到了答案,我不能在子进程中使用父进程创建的IOLoop。解决方案非常简单:

def f(storage):
    """some ioloop worker, which uses IOLoop.instance()"""
    # worker code starts here
    del IOLoop._instance

    # here we can safe use IOLoop
    IOLoop.instance().add_callback(...)
    storage.db_client.send(some value)
于 2012-06-18T22:25:00.190 回答