Home > other >  TypeError: create_task () got an unexpected keyword argument 'name'
TypeError: create_task () got an unexpected keyword argument 'name'

Time:01-06

Run into a problem when using asyncio module, the code is as follows:
 import asyncio 
Async def func () :
Print (1)
Await asyncio. Sleep (2)
Print (2)
Return "return value"


Async def main () :
Print (' main start ')
Task_list=[
Asyncio. Create_task (func (), name='t1'),
Asyncio. Create_task (func (), name='t2')
]
Print (' main end ')

Done, pending=await asyncio. Wait (task_list, timeout=None)
Print (done)

Asyncio. Run (the main ())


After the task_list, add the name error

Error message is as follows:
 D: work/study documents/crawler//protector/asyncio crawler courseware and task. The py: 12: RuntimeWarning: coroutine 'func' was never awaited 
Asyncio. Create_task (func (), name='t1'),
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (the most recent call last) :
The File "D:/work/study documents/crawler/crawler courseware protector/asyncio and task. Py", line 20, in & lt; module>
Asyncio. Run (the main ())
The File "D: \ python3.7.6 \ lib \ asyncio \ runners py", line 43, in the run
The return loop. Run_until_complete (main)
The File "D: \ python3.7.6 \ lib \ asyncio \ base_events py", line 583, in run_until_complete
Return the future. The result ()
The File "D:/work/study documents/crawler/crawler courseware protector/asyncio and task. Py", line 12, in the main
Asyncio. Create_task (func (), name='t1'),
TypeError: create_task () got an unexpected keyword argument 'name'

CodePudding user response:

1, we can see the source asyncio. Create_task () :
 def create_task (coro) : 
"" "the Schedule the execution of a coroutine object in a spawn task.

Return a Task object.
"" "
Loop=events. Get_running_loop ()
The return loop. Create_task (coro)

2, and found that the parameter, if do not have this name so change code:

 import asyncio 
Async def func () :
Print (1)
Await asyncio. Sleep (2)
Print (2)
Return "return value"


Async def main () :
Print (' main start ')
Task_list=[
Asyncio. Create_task (func ())
# asyncio. Create_task (func ())
]
Print (' main end ')

Done, pending=await asyncio. Wait (task_list, timeout=None)
Print (done)

Asyncio. Run (the main ())

3, the results are as follows:
 main start 
The main end
1
2
{& lt; The Task finished coro=& lt; Func () done, defined the at F:/python_study/main. Py: 2 & gt; Result='return value & gt; }

CodePudding user response:

reference 1st floor NoamaNelson response:
1, we can see the source code asyncio. Create_task () :
 def create_task (coro) : 
"" "the Schedule the execution of a coroutine object in a spawn task.

Return a Task object.
"" "
Loop=events. Get_running_loop ()
The return loop. Create_task (coro)

2, and found that the parameter, if do not have this name so change code:

 import asyncio 
Async def func () :
Print (1)
Await asyncio. Sleep (2)
Print (2)
Return "return value"


Async def main () :
Print (' main start ')
Task_list=[
Asyncio. Create_task (func ())
# asyncio. Create_task (func ())
]
Print (' main end ')

Done, pending=await asyncio. Wait (task_list, timeout=None)
Print (done)

Asyncio. Run (the main ())

3, the results are as follows:
 main start 
The main end
1
2
{& lt; The Task finished coro=& lt; Func () done, defined the at F:/python_study/main. Py: 2 & gt; Result='return value & gt; }

Really don't have the name is not an error, but why will add the name error, I was written according to the teacher, the teacher does not have the error, don't know what's wrong
  • Related