asyncio를 통해 일부 함수를 호출하려고합니다. 나는이 튜토리얼 http://www.giantflyingsaucer.com/blog/?p=5557을 따라 갔다. 다른 함수를 호출하는 방법에 대해서는 언급하지 않습니다.asyncio python에서 함수 호출을 실행하는 방법
import asyncio
def print_myname():
return ("My name is xyz")
def print_myage():
return ("My age is 21")
@asyncio.coroutine
def my_coroutine(future, task_name, function_call):
print("Task name", task_name)
data = yield from function_call
#yield from asyncio.get_function_source(function_call) #I was trying this too
future.set_result(data)
def got_result(future):
return future.result()
loop = asyncio.get_event_loop()
future1 = asyncio.Future()
future2 = asyncio.Future()
tasks = [
my_coroutine(future1, 'name', print_myname()),
my_coroutine(future2, 'age', print_myage())]
name = future1.add_done_callback(got_result)
age = future2.add_done_callback(got_result)
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
print ("name output", name)
print ("age output", age)
출력 할 수없는 런타임 오류가 발생합니다.
Task exception was never retrieved
future: <Task finished coro=<my_coroutine() done, defined at /home/user/Desktop/testproject/source//weather/async_t.py:11> exception=RuntimeError("Task got bad yield: 'M'",)>
Traceback (most recent call last):
result = coro.throw(exc)
File "/home/user/Desktop/testproject/source/weather/async_t.py", line 14, in my_coroutine
data = yield from function_call
RuntimeError: Task got bad yield: 'M'
예외를 통해 예외가 있지만 코드를 실행할 수없는 것으로 보입니다.
당신은 괄호가 필요합니다. 'yield from function_call()' – dirn
여전히 작동하지 않았습니다. 함수를 공동 루틴으로 변경했습니다. – Devyani