Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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 异步';s call#u稍后将引发';发电机&x27;对象不能与协同程序对象一起调用_Python_Python Asyncio - Fatal编程技术网

Python 异步';s call#u稍后将引发';发电机&x27;对象不能与协同程序对象一起调用

Python 异步';s call#u稍后将引发';发电机&x27;对象不能与协同程序对象一起调用,python,python-asyncio,Python,Python Asyncio,我用Python3.4的asyncio编写了一些简单的代码,稍后使用call\u。代码应打印,等待10秒,然后再次打印(但当应排除end()时,会引发TypeError,请参见下文): 给出了错误: Exception in callback <generator object coro at 0x7fc88eeaddc8>() handle: <TimerHandle when=31677.188005054 <generator object coro at 0x7f

我用Python3.4的asyncio编写了一些简单的代码,稍后使用
call\u
。代码应打印,等待10秒,然后再次打印(但当应排除
end()
时,会引发
TypeError
,请参见下文):

给出了错误:

Exception in callback <generator object coro at 0x7fc88eeaddc8>()
handle: <TimerHandle when=31677.188005054 <generator object coro at 0x7fc88eeaddc8>()>
Traceback (most recent call last):
  File "/usr/lib64/python3.4/asyncio/events.py", line 119, in _run
    self._callback(*self._args)
TypeError: 'generator' object is not callable
回调()中出现异常 处理: 回溯(最近一次呼叫最后一次): 文件“/usr/lib64/python3.4/asyncio/events.py”,第119行,正在运行 self.\u回调(*self.\u参数) TypeError:“生成器”对象不可调用 从docs()中可以看出,
call\u later
采用了一个coroutine对象,该对象是通过调用coroutine函数获得的。这似乎是我所做的,但是asyncio没有正确地调用
end()


这应该怎么做?

call\u later
设计用于接受回调(表示常规函数对象),而不是协同程序。较新版本的Python实际上会明确地说:

Starting to wait.
Task exception was never retrieved
future: <Task finished coro=<coro() done, defined at /usr/lib/python3.4/asyncio/coroutines.py:139> exception=TypeError('coroutines cannot be used with call_at()',)>
Traceback (most recent call last):
  File "/usr/lib/python3.4/asyncio/tasks.py", line 238, in _step
    result = next(coro)
  File "/usr/lib/python3.4/asyncio/coroutines.py", line 141, in coro
    res = func(*args, **kw)
  File "aio.py", line 6, in begin
    asyncio.get_event_loop().call_later(10, end())
  File "/usr/lib/python3.4/asyncio/base_events.py", line 392, in call_later
    timer = self.call_at(self.time() + delay, callback, *args)
  File "/usr/lib/python3.4/asyncio/base_events.py", line 404, in call_at
    raise TypeError("coroutines cannot be used with call_at()")
TypeError: coroutines cannot be used with call_at()
输出:

Starting to wait.
completed
Goodbye!
如果
end
需要是一个协同程序,那么在延迟后调用它的更自然的方法是使用
asyncio.sleep

import asyncio

@asyncio.coroutine
def begin():
    print("Starting to wait.")
    yield from asyncio.sleep(10)
    yield from end()

@asyncio.coroutine
def end():
    print("completed")

if __name__ == "__main__":
    try:
        loop = asyncio.get_event_loop()
        loop.create_task(begin())
        loop.run_forever()
    except KeyboardInterrupt:
        print("Goodbye!")
虽然从技术上讲,这是可行的:

asyncio.get_event_loop().call_later(10, lambda: asyncio.async(end()))

在这种情况下,是否有办法安排稍后使用
asyncio
调用协同路由?或者是有什么原因导致这样做没有意义?@NathanealFarley好吧,您可以稍后使用
call\u(10,lambda:asyncio.sure\u future(end())
。但是将asyncio.sleep(10)中的
yield from asyncio.sleep(10)
放在
begin
中,然后立即调用
yield from end()。如果您不想阻止
begin
,您可以将
asyncio.sleep
和调用
end
放在另一个协同程序中,然后只调用
asyncio.sure\u future(other\u coroutine())
放在
begin
中。
import asyncio

@asyncio.coroutine
def begin():
    print("Starting to wait.")
    yield from asyncio.sleep(10)
    yield from end()

@asyncio.coroutine
def end():
    print("completed")

if __name__ == "__main__":
    try:
        loop = asyncio.get_event_loop()
        loop.create_task(begin())
        loop.run_forever()
    except KeyboardInterrupt:
        print("Goodbye!")
asyncio.get_event_loop().call_later(10, lambda: asyncio.async(end()))