Python 如何在带有执行器池的异步IO代码中正确运行两个while循环?
我想同时运行两个函数。 心跳函数,即执行简单的while循环。 然后是另一个函数,它是脚本的核心,也在while循环中 当心跳功能进行时,肉和土豆功能会破坏一次又一次循环 我使用asyncio和executor池来实现两个函数(循环)同时运行的目标 另外,在使用executor池时,如何显示错误/异常,我看不到任何错误 代码如下:Python 如何在带有执行器池的异步IO代码中正确运行两个while循环?,python,multithreading,python-3.x,asynchronous,python-asyncio,Python,Multithreading,Python 3.x,Asynchronous,Python Asyncio,我想同时运行两个函数。 心跳函数,即执行简单的while循环。 然后是另一个函数,它是脚本的核心,也在while循环中 当心跳功能进行时,肉和土豆功能会破坏一次又一次循环 我使用asyncio和executor池来实现两个函数(循环)同时运行的目标 另外,在使用executor池时,如何显示错误/异常,我看不到任何错误 代码如下: import lib.myFunctions import asyncio import oandapy fmc = lib.
import lib.myFunctions
import asyncio
import oandapy
fmc = lib.myFunctions.FmcMixin()
def meatAndPotatoes(currency_pair=sys.argv[1],
time_compression=sys.argv[2],sleep_time=sys.argv[3]):
while True:
try:
something = oanda.get_history()
if (something):
print("niceeee")
else:
print("ok cool")
except:
sleep(sleep_time)
if __name__ == "__main__":
executor = ProcessPoolExecutor(2)
loop = asyncio.get_event_loop()
asyncio.async(loop.run_in_executor(executor, meatAndPotatoes))
scriptName = str(sys.argv[1]+"/"+sys.argv[2]+"/")
asyncio.async(loop.run_in_executor(executor, \
fmc.heartbeatFunction(scriptName)))
loop.run_forever()
如果两个函数都需要在
执行器中运行,并且实际上没有使用任何非阻塞I/O,则可能不应该使用asyncio
。你最好直接使用concurrent.futures.ProcessPoolExecutor
。啊,futures的东西把我甩了,因为我不需要从这些函数中返回任何东西,它们主要是输入和输出到远程服务……你能在concurrent.futures.ProcessPoolExecutor上传递任何有效的引用吗?