@@ -107,28 +107,28 @@ def task_unique_factory(cls, ctx):
107
107
async def task_unique (name , kill_me = False ):
108
108
"""Implement task.unique()."""
109
109
name = f"{ ctx .get_global_ctx_name ()} .{ name } "
110
+ curr_task = asyncio .current_task ()
110
111
if name in cls .unique_name2task :
112
+ task = cls .unique_name2task [name ]
111
113
if kill_me :
112
- #
113
- # it seems we can't cancel ourselves, so we
114
- # tell the repeaer task to cancel us
115
- #
116
- Function .task_cancel (asyncio .current_task ())
117
- # wait to be canceled
118
- await asyncio .sleep (100000 )
119
- else :
120
- task = cls .unique_name2task [name ]
121
- if task in cls .our_tasks :
122
- # only cancel tasks if they are ones we started
123
- try :
124
- task .cancel ()
125
- await task
126
- except asyncio .CancelledError :
127
- pass
128
- task = asyncio .current_task ()
129
- if task in cls .our_tasks :
130
- cls .unique_name2task [name ] = task
131
- cls .unique_task2name [task ] = name
114
+ if task != curr_task :
115
+ #
116
+ # it seems we can't cancel ourselves, so we
117
+ # tell the repeaer task to cancel us
118
+ #
119
+ Function .task_cancel (curr_task )
120
+ # wait to be canceled
121
+ await asyncio .sleep (100000 )
122
+ elif task != curr_task and task in cls .our_tasks :
123
+ # only cancel tasks if they are ones we started
124
+ try :
125
+ task .cancel ()
126
+ await task
127
+ except asyncio .CancelledError :
128
+ pass
129
+ if curr_task in cls .our_tasks :
130
+ cls .unique_name2task [name ] = curr_task
131
+ cls .unique_task2name [curr_task ] = name
132
132
133
133
return task_unique
134
134
0 commit comments