Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么get\u事件\u循环不能与运行\u in\u执行器一起使用_Python_Python 3.x_Python Asyncio - Fatal编程技术网

Python 为什么get\u事件\u循环不能与运行\u in\u执行器一起使用

Python 为什么get\u事件\u循环不能与运行\u in\u执行器一起使用,python,python-3.x,python-asyncio,Python,Python 3.x,Python Asyncio,谁能告诉我为什么下面的代码 import asyncio import time from concurrent.futures import ThreadPoolExecutor ASYNC_INTERVAL = 0.1 def plain_hello_world(name): s = "Hello world "+str(name) print(s) return s def plain_loop(name, t): start_time = time.

谁能告诉我为什么下面的代码

import asyncio
import time
from concurrent.futures import ThreadPoolExecutor

ASYNC_INTERVAL = 0.1

def plain_hello_world(name):
    s = "Hello world "+str(name)
    print(s)
    return s

def plain_loop(name, t):
    start_time = time.time()
    prev_time = start_time
    while (time.time() - start_time < t):
        if time.time() - prev_time > ASYNC_INTERVAL:
            prev_time = time.time()
            plain_hello_world(name)

def task1():
    loop = asyncio.get_event_loop()
    task = loop.run_in_executor(None, plain_loop, "func", 1)
    loop.run_until_complete(task)

def task2():
    loop = asyncio.get_event_loop()
    task = loop.run_in_executor(None, task1)
    loop.run_until_complete(task)

if __name__ == "__main__":
    task2()
虽然我不知道它有多正确或好

我不明白为什么会发生这样的错误

之所以发生这种情况,是因为task1假定它将在get_event_loop按需创建事件循环的主线程中运行,或者在先前调用set_event_loop以设置事件循环的线程中运行。由于run_in_executor在主线程以外的线程中调用其函数,并且您的代码在调用它之前不调用set_event_循环,因此会出现错误

假设task1运行100秒,我不想等待它,所以我尝试在executor中运行它,就像其他普通函数在asyncio中运行一样

运行同步函数而不等待它不是使用asyncio所做的事情,它是常规线程的工作。例如:

def任务1_背景: def工作: asyncio.set\u event\u loop asyncio.new\u event\u loop 任务1 threading.Threadtarget=work.start 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': 任务1_bg 做点别的。。。
我明白了,谢谢。线程是同步功能的唯一手段吗?我认为run_in_executor是处理这些同步函数的执行器,asyncio是线程的更好替代品也许我错了,though@adayoegirun_in_executor用于异步代码库。Asyncio不是线程的替代品,它是一种完全不同的方法。如果您的代码库已经是asyncio,那么您应该能够运行事件循环,而task1显然不允许您这样做。谢谢。我在编辑中做了修改。你认为这有意义吗?@adayoegi我被你的编辑弄糊涂了。task2实现显然在等待task1完成,因为run_until_complete。这和直接调用task1有什么区别?如果我们只有task1,结果是一样的,但是如果我们有更多的task1,结果就不同了。运行_直到_完成是我想要的。这是一个最小的示例,在实际应用程序中,它可以是一些调用task2的函数,而task2本身也是异步的
Traceback (most recent call last):
  File "asyncio_practice4.py", line 28, in <module>
    task2()
  File "asyncio_practice4.py", line 25, in task2
    loop.run_until_complete(task)
  File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
    return future.result()
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "asyncio_practice4.py", line 18, in task1
    loop = asyncio.get_event_loop()
  File "/usr/lib/python3.6/asyncio/events.py", line 694, in get_event_loop
    return get_event_loop_policy().get_event_loop()
  File "/usr/lib/python3.6/asyncio/events.py", line 602, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.
def task2():
    loop = asyncio.get_event_loop()
    def wrapper():
        asyncio.set_event_loop(asyncio.new_event_loop())
        task1()
    task = loop.run_in_executor(None, wrapper)
    loop.run_until_complete(task)