Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def queue_work(
self,
func: Callable[..., Awaitable[R]],
args: Sequence[Any],
kwargs: Dict[str, Any],
) -> TaskID:
"""Add a new work item to the outgoing queue."""
self.last_id += 1
task_id = TaskID(self.last_id)
qid = self.scheduler.schedule_task(task_id, func, args, kwargs)
tx, _ = self.queues[qid]
tx.put_nowait((task_id, func, args, kwargs))
return task_id
async def results(self, tids: Sequence[TaskID]) -> Sequence[R]:
"""Wait for all tasks to complete, and return results, preserving order."""
pending = set(tids)
ready: Dict[TaskID, R] = {}
while pending:
for tid in pending.copy():
if tid in self._results:
result, tb = self._results.pop(tid)
if tb is not None:
raise ProxyException(tb)
ready[tid] = result
pending.remove(tid)
await asyncio.sleep(0.005)
return [ready[tid] for tid in tids]