我的项目使用eventlet,现在我必须异步读写一个文件(实际上是一个设备)。我尝试使用eventlet.tpool.execute()来运行读取线程,但它阻塞了主循环。
我的问题是,如何同时运行读取线程和eventlet线程?这两个线程是否有可能以某种方式进行通信?
一个简单的草图:
def functionB():
while True:
data = readFile()
doSomethingWith(data)
def functionA():
doSomething()
tpool.execute(functionB)
doSomethingElse()则永远不会调用doSomethingElse()。
发布于 2013-09-25 22:39:50
在functionB结束之前,tpool.execute应该不会返回。您的代码不会结束,所以doSomethingElse()不应该执行。
换句话说,tpool.execute不是“一发即忘”的类型。它在OS线程中产生函数,并同步调用者。实际上,这非常有用。
如果你想启动一个新的永久工作线程,只需使用普通的Python threading.Thread即可。
tpool.execute的典型用例
def main():
f = tpool.execute(open, ...)
while True:
chunk = tpool.execute(f.read, size)
# process chunk
tpool.execute(f.close)您可以尝试以下代码来解决您的问题:
def functionB():
while True:
data = tpool.execute(readFile) # just readFile -> tpool.execute
doSomethingWith(data)
def functionA():
doSomething()
eventlet.spawn_n(functionB) # tpool.execute -> spawn_n
doSomethingElse()https://stackoverflow.com/questions/18984178
复制相似问题