Python 与系统调用协作eventlet

Python 与系统调用协作eventlet,python,eventlet,Python,Eventlet,我的项目使用eventlet,现在我必须异步读写文件(实际上是一个设备)。我尝试了eventlet.tpool.execute()来运行读取线程,但它阻止了主循环 我的问题是,如何与eventlet线程同时运行reading线程?这两个线程是否可以通过某种方式进行通信 速写: def functionB(): while True: data = readFile() doSomethingWith(data) def functionA(): doSomething(

我的项目使用
eventlet
,现在我必须异步读写文件(实际上是一个设备)。我尝试了
eventlet.tpool.execute()
来运行读取线程,但它阻止了主循环

我的问题是,如何与eventlet线程同时运行reading线程?这两个线程是否可以通过某种方式进行通信

速写:

def functionB():
  while True:
    data = readFile()
    doSomethingWith(data)

def functionA():
  doSomething()
  tpool.execute(functionB)
  doSomethingElse()

然后再也不会调用
doSomethingElse()

tpool.execute在
函数b
结束之前不应返回。您的任务没有结束,因此不应该执行
doSomethingElse()

换句话说,
tpool.execute
不是激发和遗忘类型。它在操作系统线程中生成函数并同步调用者。实际上,这是非常有用的

如果您想启动一个新的永久工作线程,只需使用普通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()

你能提供更多关于“阻塞主回路”部分的细节吗?另外,最少的复制代码也会很有帮助。@temoto添加了一个快速的草图,希望你能理解我的意思。没错,但是
tpool
假设是一个线程池吗?那么,我应该怎么做才能使
tpool.execute()
不阻止后续调用呢?池部分意味着可以并发工作的线程很少,也就是说,您可以从多个绿色线程运行
tpool.execute
,它们最多执行N个(池大小)并发作业。池有两个关键属性:重用以前初始化的资源和限制要创建的资源数量。在这个上下文中,资源是一个OS线程。若你们想在后台执行一件事情,你们不需要线程池。只需启动
线程。线程
(假设您没有使用猴子补丁线程)。这就是问题所在。主线程是由eventlet生成的线程,我无法控制该部分。