some async tests
unknown
python
7 months ago
2.8 kB
2
Indexable
Never
# možné řešení pro cancel_tasks async def cancel_tasks(self): # atasks = asyncio.all_tasks() # current = asyncio.current_task() # atasks.remove(current) # for atask in atasks: # atask.cancel() # for atask in atasks: # try: # await atask # except asyncio.CancelledError: # pass cancelled_atasks = [] while self.inprogress_tasks: path, task = self.inprogress_tasks.popitem() task.asyncio_task.cancel() cancelled_atasks.append(task.asyncio_task) for atask in cancelled_atasks: try: await atask except asyncio.CancelledError: pass # špatné řešení pro handle_urls async def handle_urls(self): atasks = [] while self.inprogress_tasks: for path, task in self.inprogress_tasks.items(): atasks.append(task.asyncio_task) break print(atasks) try: atask_group = asyncio.gather(*atasks) await atask_group except Exception as exc: current_atask = asyncio.current_task() task_path = current_atask.get_name() task = self.inprogress_tasks.get(task_path, None) assert task is not None del self.inprogress_tasks[task.path] self.failed_tasks[task.path] = task self.call_hook('page_failed', hooks.TaskInfo(task)) if self.fail_fast: raise exc if path in self.inprogress_tasks: raise ValueError(f'{task} is in_progress after it was handled') # jiné špatné řešení pro handle_urls async def handle_urls(self): while self.inprogress_tasks: for path, task in self.inprogress_tasks.items(): atasks.append(task.asyncio_task) break print(atasks) try: atask_group = asyncio.gather(*atasks) await atask_group except Exception as exc: all_atasks = asyncio.all_tasks() for atask in all_atasks: if atask.exception(): # atask with exception task_path = atask.get_name() break #current_atask = asyncio.current_task() all_atasks.remove(atask) task = self.inprogress_tasks.get(task_path, None) assert task is not None del self.inprogress_tasks[task.path] self.failed_tasks[task.path] = task self.call_hook('page_failed', hooks.TaskInfo(task)) if self.fail_fast: raise exc if path in self.inprogress_tasks: raise ValueError(f'{task} is in_progress after it was handled')