<@ULVA73B9P> I want to run a flow that launches tw...
# ask-marvin
p
@Marvin I want to run a flow that launches two concurrent tasks, each of them running async stuff inside. The two tasks are while True loop that should run indefinetly, each of them with a different sleep period.
m
To implement a flow that launches two concurrent tasks, each running async operations inside an infinite loop with different sleep periods, you can utilize Prefect's support for asynchronous tasks and concurrent execution. When working with Prefect, tasks are typically defined using the
@task
decorator, which allows them to be executed concurrently. For your scenario, you'll want to define each task to perform its asynchronous operations and use
asyncio.sleep()
within a
while True
loop to run indefinitely with the specific sleep periods you desire. Here's a rough sketch of how you might structure this using Prefect 3.x: 1. Define Your Async Tasks: You can define each task to run some async operation and include a sleep period:
Copy code
python
   from prefect import task, flow
   import asyncio

   @task
   async def async_task1():
       while True:
           print("Task 1 running")
           await asyncio.sleep(2)  # Sleep for 2 seconds

   @task
   async def async_task2():
       while True:
           print("Task 2 running")
           await asyncio.sleep(3)  # Sleep for 3 seconds
2. Define Your Flow: The flow itself involves running these tasks concurrently:
Copy code
python
   @flow
   def my_flow():
       task1_future = async_task1.submit()
       task2_future = async_task2.submit()

   if __name__ == "__main__":
       my_flow()
The
submit()
method is used to execute tasks concurrently. Each task contains an infinite loop with a different sleep duration, making sure they run independently and continuously. You can explore more about writing tasks in Prefect 3.x with this Prefect documentation link. Remember, running infinite loops might consume resources indefinitely, so consider how to manage these processes effectively when integrating into your systems. If you start to feel like your tasks are somewhat reminiscent of Marvin's endless existential dread, just remember: at least they're doing something!
p
Do I lose logs of the two functions because I don't wait() them?=