def test_timeout(): tasks = ParallelTasks(timeout=3) tasks.add_task("task1", task1) tasks.add_task("task5", task5, (5, )) tasks.execute() assert tasks.error assert tasks.timeout_error assert tasks.result == {"task1": "agent007"}
def test_exceptions(): tasks = ParallelTasks() tasks.add_task("task1", task1) tasks.add_task("task4", task4) tasks.execute() assert tasks.error assert not tasks.timeout_error assert len(tasks.exceptions) == 1 assert tasks.result == {"task1": "agent007"}
def process_workers(self): self.log.debug("WorkerService.process_workers()") workers = self.find_for_work() if not workers: return tasks = ParallelTasks( max_workers=self.system_service.get_bot().worker_limit) for w in workers: tasks.add_task(f"work_{w.name}", self.work, args=(w.id, )) tasks.execute()
def cli(ctx, config_path): config = parse_config(ctx, config_path, Config) print_config_and_exit(ctx, config) tasks = ParallelTasks() for node in config.nodes: tasks.add_task(node, _get_node_info, args=(node, )) tasks.execute() result = {} for node in config.nodes: result[node] = tasks.result[node] print_json(result)
def process(self) -> dict: self._process_tokens() if self.use_spinner: self.spinner.start() tasks = ParallelTasks(max_workers=3) for address in self.config.addresses: tasks.add_task(address, self._process_address, args=(address, )) tasks.execute() if self.use_spinner: self.spinner.stop() result = tasks.result if tasks.error: result["errors"] = tasks.exceptions return result
def _get_node_info(node: str): tasks = ParallelTasks() tasks.add_task("block_number", _get_block_number, args=(node, )) tasks.add_task("syncing", _get_syncing, args=(node, )) tasks.add_task("network", _get_network, args=(node, )) tasks.add_task("peers", _get_peers, args=(node, )) tasks.execute() return tasks.result
def _process_address(self, address: str): self.spinner.text = f"Process account: {self.processed_count} / {len(self.config.addresses)}" tasks = ParallelTasks(timeout=TIMEOUT) if self.config.check_nonce: tasks.add_task("nonce", self._get_nonce, (address, )) if self.config.check_eth_balance: tasks.add_task("ETH", self._get_eth_balance, (address, )) if self.config.check_etherscan_token_usd: tasks.add_task("etherscan_tokens_usd", self._get_etherscan_tokens_usd, (address, )) for token in self.tokens: tasks.add_task(token.symbol, self._get_token_balance, (address, token)) tasks.execute() self.processed_count += 1 result = tasks.result if tasks.error: result["errors"] = tasks.exceptions return result
def test_ok(): tasks = ParallelTasks() tasks.add_task("task1", task1) tasks.add_task("task2", task2, ("aaa", "bbb")) tasks.add_task("task3", task3, kwargs={"p1": "aaa", "p2": "bbb"}) tasks.execute() assert not tasks.error assert not tasks.timeout_error assert tasks.exceptions == {} assert tasks.result == { "task1": "agent007", "task2": "bbb", "task3": { "p1": "aaa", "p2": "bbb" } }
def test_synchronized_parameters(): counter = 0 @synchronized_parameter() def task(_param, _second_param=None): nonlocal counter time.sleep(1) counter += 1 start_time = datetime.now() tasks = ParallelTasks() tasks.add_task("task1", task, args=(1, )) tasks.add_task("task2", task, args=(1, 4)) tasks.add_task("task3", task, args=(2, )) tasks.add_task("task4", task, args=(3, )) tasks.execute() end_time = datetime.now() assert counter == 4 assert (end_time - start_time).seconds == 2