My project uses eventlet
and now I have to asynchronously read and write to a file(a device, actually). I tried eventlet.tpool.execute()
to run the reading thread, but it blocks the main loop.
My question is, how to run the reading thread concurrently with the eventlet thread? Is it possible for these two threads to communicate in some ways?
A quick sketch:
def functionB():
while True:
data = readFile()
doSomethingWith(data)
def functionA():
doSomething()
tpool.execute(functionB)
doSomethingElse()
Then doSomethingElse()
is never called.
tpool.execute
is not supposed to return until functionB
ended. Yours does not end, so doSomethingElse()
is not supposed to execute.
In other words, tpool.execute
is not fire-and-forget type. It spawns function in OS thread and synchronizes caller. Which is very useful, actually.
If you want to start a new forever working thread, just do it with normal Python threading.Thread
.
Typical use case for tpool.execute
:
def main():
f = tpool.execute(open, ...)
while True:
chunk = tpool.execute(f.read, size)
# process chunk
tpool.execute(f.close)
You may try the following code to fix your problem:
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.