Python asyncio.create_task() Examples

The following are 30 code examples of asyncio.create_task(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module asyncio , or try the search function .
Example #1
Source File: help_channels.py    From bot with MIT License 7 votes vote down vote up
def __init__(self, bot: Bot):
        super().__init__()

        self.bot = bot

        # Categories
        self.available_category: discord.CategoryChannel = None
        self.in_use_category: discord.CategoryChannel = None
        self.dormant_category: discord.CategoryChannel = None

        # Queues
        self.channel_queue: asyncio.Queue[discord.TextChannel] = None
        self.name_queue: t.Deque[str] = None

        self.name_positions = self.get_names()
        self.last_notification: t.Optional[datetime] = None

        # Asyncio stuff
        self.queue_tasks: t.List[asyncio.Task] = []
        self.ready = asyncio.Event()
        self.on_message_lock = asyncio.Lock()
        self.init_task = self.bot.loop.create_task(self.init_cog()) 
Example #2
Source File: reminders.py    From bot with MIT License 6 votes vote down vote up
def ensure_valid_reminder(
        self,
        reminder: dict,
        cancel_task: bool = True
    ) -> t.Tuple[bool, discord.User, discord.TextChannel]:
        """Ensure reminder author and channel can be fetched otherwise delete the reminder."""
        user = self.bot.get_user(reminder['author'])
        channel = self.bot.get_channel(reminder['channel_id'])
        is_valid = True
        if not user or not channel:
            is_valid = False
            log.info(
                f"Reminder {reminder['id']} invalid: "
                f"User {reminder['author']}={user}, Channel {reminder['channel_id']}={channel}."
            )
            asyncio.create_task(self._delete_reminder(reminder['id'], cancel_task))

        return is_valid, user, channel 
Example #3
Source File: adapters.py    From ptadapter with GNU General Public License v3.0 6 votes vote down vote up
def start(self) -> None:
        """(async) Start the PT executable and wait until it's ready.

        "Ready" means that all transports have finished initializing.
        """
        self._check_not_started()
        await self._pre_start()
        env = self._build_env()
        self._logger.debug('PT environment variables: %r', env)
        self._process = await asyncio.create_subprocess_exec(
            *self._pt_args,
            env=env,
            stdin=asyncio.subprocess.PIPE,
            stdout=asyncio.subprocess.PIPE,
            stderr=None,
        )
        self._logger.debug('Started PT subprocess: %r', self._process)
        self._stdout_task = asyncio.create_task(self._process_stdout())
        try:
            await self._ready
        except Exception:
            await self.stop()
            raise 
Example #4
Source File: session.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def init(app: web.Application) -> None:
    event_dispatcher = app['event_dispatcher']
    event_dispatcher.consume('kernel_preparing', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_pulling', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_creating', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_started', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_terminating', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_terminated', app, handle_kernel_lifecycle)
    event_dispatcher.consume('kernel_success', app, handle_batch_result)
    event_dispatcher.consume('kernel_failure', app, handle_batch_result)
    event_dispatcher.consume('kernel_stat_sync', app, handle_kernel_stat_sync)
    event_dispatcher.consume('kernel_log', app, handle_kernel_log)
    event_dispatcher.consume('instance_started', app, handle_instance_lifecycle)
    event_dispatcher.consume('instance_terminated', app, handle_instance_lifecycle)
    event_dispatcher.consume('instance_heartbeat', app, handle_instance_heartbeat)
    event_dispatcher.consume('instance_stats', app, handle_instance_stats)

    app['pending_waits'] = set()

    # Scan ALIVE agents
    app['agent_lost_checker'] = aiotools.create_timer(
        functools.partial(check_agent_lost, app), 1.0)
    app['stats_task'] = asyncio.create_task(stats_report_timer(app)) 
Example #5
Source File: test_barrier.py    From aiozk with MIT License 6 votes vote down vote up
def test_wait_before_create(zk, path):
    """await barrier.wait() should finish immediately if the barrier does not
    exist. Because it is semantically right: No barrier, no blocking.
    """
    wait_finished = False

    async def start_worker():
        barrier = zk.recipes.Barrier(path)
        await barrier.wait()
        nonlocal wait_finished
        wait_finished = True

    task = asyncio.create_task(start_worker())

    try:
        await asyncio.wait_for(task, timeout=2)
    except asyncio.TimeoutError:
        pass

    assert wait_finished 
Example #6
Source File: qrcode.py    From specter-diy with MIT License 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.label = lv.label(self)
        self._text = "Text"
        self.label.set_long_mode(lv.label.LONG.BREAK)
        self.label.set_align(lv.label.ALIGN.CENTER)

        self.note = lv.label(self)
        style = lv.style_t()
        lv.style_copy(style, qr_style)
        style.text.font = lv.font_roboto_16
        style.text.color = lv.color_hex(0x192432)
        self.note.set_style(0, style)
        self.note.set_text("")
        self.note.set_align(lv.label.ALIGN.CENTER)

        self.set_text(self._text)
        self.task = asyncio.create_task(self.animate())
        self.set_event_cb(self.cb) 
Example #7
Source File: port.py    From edgedb with Apache License 2.0 6 votes vote down vote up
def stop(self):
        try:
            srv = self._http_proto_server
            if srv is not None:
                self._http_proto_server = None
                srv.close()
                await srv.wait_closed()
        finally:
            try:
                async with taskgroup.TaskGroup() as g:
                    for cmp in self._compilers_list:
                        g.create_task(cmp.close())
                    self._compilers_list.clear()

                for pgcon in self._pgcons_list:
                    pgcon.terminate()
                self._pgcons_list.clear()
                if self._http_request_logger is not None:
                    self._http_request_logger.cancel()
                    await self._http_request_logger
            finally:
                await super().stop() 
Example #8
Source File: test_election.py    From aiozk with MIT License 6 votes vote down vote up
def test_election_early_wait_for_leadership(zk, path):
    elec = zk.recipes.LeaderElection(path)

    early_wait_success = asyncio.Event()

    async def wait_early():
        await elec.wait_for_leadership()
        assert elec.has_leadership
        early_wait_success.set()

    asyncio.create_task(wait_early())
    await asyncio.sleep(0.5)
    assert not elec.has_leadership

    await elec.volunteer()

    # NO WAIT
    await asyncio.wait_for(early_wait_success.wait(), timeout=0.5)

    await elec.resign()

    assert not elec.has_leadership

    await zk.delete(path) 
Example #9
Source File: scheduling.py    From bot with MIT License 6 votes vote down vote up
def schedule_task(self, task_id: t.Hashable, task_data: t.Any) -> None:
        """
        Schedules a task.

        `task_data` is passed to the `Scheduler._scheduled_task()` coroutine.
        """
        log.trace(f"{self.cog_name}: scheduling task #{task_id}...")

        if task_id in self._scheduled_tasks:
            log.debug(
                f"{self.cog_name}: did not schedule task #{task_id}; task was already scheduled."
            )
            return

        task = asyncio.create_task(self._scheduled_task(task_data))
        task.add_done_callback(partial(self._task_done_callback, task_id))

        self._scheduled_tasks[task_id] = task
        log.debug(f"{self.cog_name}: scheduled task #{task_id} {id(task)}.") 
Example #10
Source File: test_asyncio.py    From python-sensor with MIT License 6 votes vote down vote up
def test_create_task_without_context(self):
            async def run_later(msg="Hello"):
                # print("run_later: %s" % async_tracer.active_span.operation_name)
                async with aiohttp.ClientSession() as session:
                    return await self.fetch(session, testenv["wsgi_server"] + "/")

            async def test():
                with async_tracer.start_active_span('test'):
                    asyncio.create_task(run_later("Hello"))
                await asyncio.sleep(0.5)

            self.loop.run_until_complete(test())

            spans = self.recorder.queued_spans()

            self.assertEqual(2, len(spans))
            self.assertEqual("sdk", spans[0].n)
            self.assertEqual("wsgi", spans[1].n)

            # Without the context propagated, we should get two separate traces
            self.assertNotEqual(spans[0].t, spans[1].t) 
Example #11
Source File: local_client_base.py    From galaxy_blizzard_plugin with MIT License 6 votes vote down vote up
def __init__(self, update_statuses):
        self._update_statuses = update_statuses
        self._process_provider = ProcessProvider()
        self._process = None
        self._exe = self._find_exe()
        self._games_provider = LocalGames()

        self.database_parser = None
        self.config_parser = ConfigParser(None)
        self.uninstaller = None
        self.installed_games_cache = self.get_installed_games()

        loop = asyncio.get_event_loop()
        loop.create_task(self._register_local_data_watcher())
        loop.create_task(self._register_classic_games_updater())
        self.classic_games_parsing_task = None 
Example #12
Source File: decorators.py    From fontgoggles with Apache License 2.0 6 votes vote down vote up
def asyncTaskAutoCancel(func):
    """Wraps an async method into a regular method, that will schedule
    the async function as a task. If this task has previously been scheduled
    and has not yet run, it will be cancelled. So a newly scheduled task
    overrides an older one.
    """
    taskAttributeName = f"_{func.__name__}_autoCancelTask"
    @functools.wraps(func)
    def createFuncTask(self, *args, **kwargs):
        oldTask = getattr(self, taskAttributeName, None)
        if oldTask is not None:
            oldTask.cancel()
        coro = func(self, *args, **kwargs)
        task = asyncio.create_task(coro)
        task.add_done_callback(_done_callback)
        setattr(self, taskAttributeName, task)
        return task
    return createFuncTask 
Example #13
Source File: confserver.py    From bumper with GNU General Public License v3.0 6 votes vote down vote up
def restart_MQTT(self):
        
        if not (bumper.mqtt_server.broker.transitions.state == "stopped" or bumper.mqtt_server.broker.transitions.state == "not_started"):
            # close session writers - this was required so bots would reconnect properly after restarting
            for sess in list(bumper.mqtt_server.broker._sessions):                
                sessobj = bumper.mqtt_server.broker._sessions[sess][1]
                if sessobj.session.transitions.state == "connected":
                    await sessobj.writer.close()

            #await bumper.mqtt_server.broker.shutdown()
            aloop = asyncio.get_event_loop()
            aloop.call_later(
            0.1, lambda: asyncio.create_task(bumper.mqtt_server.broker.shutdown())
            )  # In .1 seconds shutdown broker

        
        aloop = asyncio.get_event_loop()
        aloop.call_later(
           1.5, lambda: asyncio.create_task(bumper.mqtt_server.broker_coro())
        )  # In 1.5 seconds start broker 
Example #14
Source File: confserver.py    From bumper with GNU General Public License v3.0 6 votes vote down vote up
def handle_RestartService(self, request):
        try:
            service = request.match_info.get("service", "")
            if service == "Helperbot":
                await self.restart_Helper()
                return web.json_response({"status": "complete"})
            elif service == "MQTTServer":
                asyncio.create_task(self.restart_MQTT())
                aloop = asyncio.get_event_loop()
                aloop.call_later(
                    5, lambda: asyncio.create_task(self.restart_Helper())
                )  # In 5 seconds restart Helperbot
                
                return web.json_response({"status": "complete"})
            elif service == "XMPPServer":
                await self.restart_XMPP()
                return web.json_response({"status": "complete"})
            else:
                return web.json_response({"status": "invalid service"})

        except Exception as e:
            confserverlog.exception("{}".format(e))
            pass 
Example #15
Source File: mqttserver.py    From bumper with GNU General Public License v3.0 6 votes vote down vote up
def broker_coro(self):

        mqttserverlog.info(
            "Starting MQTT Server at {}:{}".format(self.address[0], self.address[1])
        )        

        try:
            await self.broker.start()

        except hbmqtt.broker.BrokerException as e:
            mqttserverlog.exception(e)
            #asyncio.create_task(bumper.shutdown())
            pass

        except Exception as e:
            mqttserverlog.exception("{}".format(e))
            #asyncio.create_task(bumper.shutdown())
            pass 
Example #16
Source File: test_init.py    From bumper with GNU General Public License v3.0 6 votes vote down vote up
def test_start_stop():
    with LogCapture() as l:
        if os.path.exists("tests/tmp.db"):
            os.remove("tests/tmp.db")  # Remove existing db

        b = bumper
        b.db = "tests/tmp.db"  # Set db location for testing
        b.conf1_listen_address = "127.0.0.1"
        b.conf1_listen_port = 444
        asyncio.create_task(b.start())
        await asyncio.sleep(0.1)
        l.check_present(("bumper", "INFO", "Starting Bumper"))
        l.clear()

        asyncio.create_task(b.shutdown())
        await asyncio.sleep(0.1)
        l.check_present(
            ("bumper", "INFO", "Shutting down"), ("bumper", "INFO", "Shutdown complete")
        )
        assert b.shutting_down == True 
Example #17
Source File: test_init.py    From bumper with GNU General Public License v3.0 6 votes vote down vote up
def test_start_stop_debug():
    with LogCapture() as l:
        if os.path.exists("tests/tmp.db"):
            os.remove("tests/tmp.db")  # Remove existing db

        b = bumper
        b.db = "tests/tmp.db"  # Set db location for testing
        b.bumper_listen = "0.0.0.0"
        b.bumper_debug = True
        asyncio.create_task(b.start())

        await asyncio.sleep(0.1)
        asyncio.create_task(b.shutdown())
        l.check_present(("bumper", "INFO", "Starting Bumper"))
        l.clear()
        await asyncio.sleep(0.1)
        l.check_present(
            ("bumper", "INFO", "Shutting down"), ("bumper", "INFO", "Shutdown complete")
        )
        assert b.shutting_down == True 
Example #18
Source File: worker.py    From mergify-engine with Apache License 2.0 6 votes vote down vote up
def _run(self):
        self._stopping.clear()

        self._redis = await utils.create_aredis_for_stream()
        self._stream_selector = StreamSelector(self.worker_count, self._redis)

        if "stream" in self.enabled_services:
            for i in range(self.worker_count):
                self._worker_tasks.append(
                    asyncio.create_task(self.stream_worker_task(i))
                )

        if "stream-monitoring" in self.enabled_services:
            self._stream_monitoring_task = asyncio.create_task(self.monitoring_task())

        if "smart-queue" in self.enabled_services:
            self._smart_queue_task = asyncio.create_task(
                self.smart_queue_processing_task()
            )

        LOG.debug("%d workers spawned", self.worker_count) 
Example #19
Source File: worker.py    From mergify-engine with Apache License 2.0 5 votes vote down vote up
def stop(self):
        self._stop_task = asyncio.create_task(self._shutdown()) 
Example #20
Source File: plugin.py    From galaxy_blizzard_plugin with MIT License 5 votes vote down vote up
def _update_statuses(self, refreshed_games, previous_games):
        if not self.local_games_called:
            return
        for blizz_id, refr in refreshed_games.items():
            prev = previous_games.get(blizz_id, None)

            if prev is None:
                if refr.playable:
                    log.debug('Detected playable game')
                    state = LocalGameState.Installed
                else:
                    log.debug('Detected installation begin')
                    state = LocalGameState.None_
            elif refr.playable and not prev.playable:
                log.debug('Detected playable game')
                state = LocalGameState.Installed
            elif refr.last_played != prev.last_played:
                log.debug('Detected launched game')
                state = LocalGameState.Installed | LocalGameState.Running
                asyncio.create_task(self._notify_about_game_stop(refr, 5))
            else:
                continue

            log.info(f'Changing game {blizz_id} state to {state}')
            self.update_local_game_status(LocalGame(blizz_id, state))

        for blizz_id, prev in previous_games.items():
            refr = refreshed_games.get(blizz_id, None)
            if refr is None:
                log.debug('Detected uninstalled game')
                state = LocalGameState.None_
                self.update_local_game_status(LocalGame(blizz_id, state)) 
Example #21
Source File: receive_message.py    From azure-iot-sdk-python with MIT License 5 votes vote down vote up
def main():
    # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here.
    conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
    # The client object is used to interact with your Azure IoT hub.
    device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)

    # connect the client.
    await device_client.connect()

    # define behavior for receiving a message
    async def message_listener(device_client):
        while True:
            message = await device_client.receive_message()  # blocking call
            print("the data in the message received was ")
            print(message.data)
            print("custom properties are")
            print(message.custom_properties)
            print("content Type: {0}".format(message.content_type))
            print("")

    # define behavior for halting the application
    def stdin_listener():
        while True:
            selection = input("Press Q to quit\n")
            if selection == "Q" or selection == "q":
                print("Quitting...")
                break

    # Schedule task for message listener
    asyncio.create_task(message_listener(device_client))

    # Run the stdin listener in the event loop
    loop = asyncio.get_running_loop()
    user_finished = loop.run_in_executor(None, stdin_listener)

    # Wait for user to indicate they are done listening for messages
    await user_finished

    # Finally, disconnect
    await device_client.disconnect() 
Example #22
Source File: receive_twin_desired_properties_patch.py    From azure-iot-sdk-python with MIT License 5 votes vote down vote up
def main():
    # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here.
    conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
    # The client object is used to interact with your Azure IoT hub.
    device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)

    # connect the client.
    await device_client.connect()

    # define behavior for receiving a twin patch
    async def twin_patch_listener(device_client):
        while True:
            patch = await device_client.receive_twin_desired_properties_patch()  # blocking call
            print("the data in the desired properties patch was: {}".format(patch))

    # define behavior for halting the application
    def stdin_listener():
        while True:
            selection = input("Press Q to quit\n")
            if selection == "Q" or selection == "q":
                print("Quitting...")
                break

    # Schedule task for twin patch
    asyncio.create_task(twin_patch_listener(device_client))

    # Run the stdin listener in the event loop
    loop = asyncio.get_running_loop()
    user_finished = loop.run_in_executor(None, stdin_listener)

    # Wait for user to indicate they are done listening for messages
    await user_finished

    # Finally, disconnect
    await device_client.disconnect() 
Example #23
Source File: worker.py    From mergify-engine with Apache License 2.0 5 votes vote down vote up
def start(self):
        self._start_task = asyncio.create_task(self._run()) 
Example #24
Source File: plugin.py    From galaxy_blizzard_plugin with MIT License 5 votes vote down vote up
def launch_game(self, game_id):
        if not self.local_games_called:
            await self.get_local_games()

        try:
            game = self.local_client.get_installed_games().get(game_id, None)
            if game is None:
                log.error(f'Launching game that is not installed: {game_id}')
                return await self.install_game(game_id)

            if isinstance(game.info, ClassicGame):
                log.info(f'Launching game of id: {game_id}, {game} at path {os.path.join(game.install_path, game.info.exe)}')
                if SYSTEM == pf.WINDOWS:
                    subprocess.Popen(os.path.join(game.install_path, game.info.exe))
                elif SYSTEM == pf.MACOS:
                    if not game.info.bundle_id:
                        log.warning(f"{game.name} has no bundle id, help by providing us bundle id of this game")
                    subprocess.Popen(['open', '-b', game.info.bundle_id])

                self.update_local_game_status(LocalGame(game_id, LocalGameState.Installed | LocalGameState.Running))
                asyncio.create_task(self._notify_about_game_stop(game, 6))
                return

            self.local_client.refresh()
            log.info(f'Launching game of id: {game_id}, {game}')
            await self.local_client.launch_game(game, wait_sec=60)

            self.update_local_game_status(LocalGame(game_id, LocalGameState.Installed | LocalGameState.Running))
            self.local_client.close_window()
            asyncio.create_task(self._notify_about_game_stop(game, 3))

        except ClientNotInstalledError as e:
            log.warning(e)
            await self.open_battlenet_browser()
        except TimeoutError as e:
            log.warning(str(e))
        except Exception as e:
            log.exception(f"Launching game {game_id} failed: {e}") 
Example #25
Source File: plugin.py    From galaxy_blizzard_plugin with MIT License 5 votes vote down vote up
def log_out(self):
        if self.backend_client:
            asyncio.create_task(self.authentication_client.shutdown())
        self.authentication_client.user_details = None 
Example #26
Source File: persistence.py    From protoactor-python with Apache License 2.0 5 votes vote down vote up
def receive(self, context: AbstractContext) -> None:
        message = context.message
        if isinstance(message, Started):
            print('LoopActor - Started')
            await context.send(context.my_self, LoopParentMessage())
        elif isinstance(message, LoopParentMessage):
            async def fn():
                await context.send(context.parent, RenameCommand(name=self.generate_pronounceable_name(5)))
                await asyncio.sleep(0.5)
                await context.send(context.my_self, LoopParentMessage())

            asyncio.create_task(fn()) 
Example #27
Source File: watcher.py    From galaxy_blizzard_plugin with MIT License 5 votes vote down vote up
def __init__(self, path, event, interval):
        self.path = path
        self.event = event
        self.last_modification_time = None
        self.interval = interval
        self.task = asyncio.create_task(self._watcher()) 
Example #28
Source File: dirrec.py    From FinalRecon with MIT License 5 votes vote down vote up
def wayback(dserv, tout):
	global found
	print('\n' + Y + '[!]' + C + ' Requesting Wayback Machine...' + W + '\n')
	tasks = []
	resolver = aiohttp.AsyncResolver(nameservers=[dserv])
	conn = aiohttp.TCPConnector(limit=10)
	timeout = aiohttp.ClientTimeout(total=None, sock_connect=tout, sock_read=tout)
	async with aiohttp.ClientSession(connector=conn, timeout=timeout) as session:
		for f_url in found:
			tasks.append(asyncio.create_task(wm_fetch(f_url, session)))
		await asyncio.gather(*tasks) 
Example #29
Source File: dirrec.py    From FinalRecon with MIT License 5 votes vote down vote up
def run(target, threads, tout, wdlist, redir, sslv, dserv, output, data, filext):
	global responses
	tasks = []
	if len(filext) == 0:
		url = target + '/{}'
		resolver = aiohttp.AsyncResolver(nameservers=[dserv])
		conn = aiohttp.TCPConnector(limit=threads, resolver=resolver, family=socket.AF_INET, verify_ssl=sslv)
		timeout = aiohttp.ClientTimeout(total=None, sock_connect=tout, sock_read=tout)
		async with aiohttp.ClientSession(connector=conn, timeout=timeout) as session:
			with open(wdlist, 'r') as wordlist:
				for word in wordlist:
					word = word.strip()
					task = asyncio.create_task(fetch(url.format(word), session, redir, sslv))
					tasks.append(task)
					await asyncio.sleep(0)
			responses = await asyncio.gather(*tasks)
	else:
		filext = ',' + filext
		filext = filext.split(',')
		for ext in filext:
			ext = ext.strip()
			if len(ext) == 0:
				url = target + '/{}'
			else:
				url = target + '/{}.' + ext
			resolver = aiohttp.AsyncResolver(nameservers=[dserv])
			conn = aiohttp.TCPConnector(limit=threads, resolver=resolver, family=socket.AF_INET, verify_ssl=sslv)
			timeout = aiohttp.ClientTimeout(total=None, sock_connect=tout, sock_read=tout)
			async with aiohttp.ClientSession(connector=conn, timeout=timeout) as session:
				with open(wdlist, 'r') as wordlist:
					for word in wordlist:
						word = word.strip()
						task = asyncio.create_task(fetch(url.format(word), session, redir, sslv))
						tasks.append(task)
						await asyncio.sleep(0)
				responses = await asyncio.gather(*tasks) 
Example #30
Source File: decorators.py    From fontgoggles with Apache License 2.0 5 votes vote down vote up
def asyncTask(func):
    """Wraps an async function or method into a regular function, that
    will schedule the async function as a task. Returns an asyncio Task
    object, that can be cancelled.
    """
    @functools.wraps(func)
    def createFuncTask(*args, **kwargs):
        coro = func(*args, **kwargs)
        task = asyncio.create_task(coro)
        task.add_done_callback(_done_callback)
        return task
    return createFuncTask