Home > Net >  Stop Python from Terminating As Long As Background Async Task is Running
Stop Python from Terminating As Long As Background Async Task is Running

Time:02-01

I've been looking around and have seen a lot of information regarding asyncio. I'm having trouble creating a program that won't terminate as long as the background task is running.

def loop_test():
    print("task is running")
    time.sleep(2)
    print("task is finished")

async def start_pipeline(self):

    print("Starting TD Stream")

    # Build data pipeline
    await self.td_stream_client.build_pipeline()

    data_response_count = 0
    self.streaming = True

    # Keep going while receiving data
    while self.streaming:
        print("Streaming")
        data = await self.td_stream_client.start_pipeline()

        # Parse if data inside
        if 'data' in data:
            content = data['data'][0]['content']

            print("Key: {}".format(content[0]['key']))
            pprint.pprint(content, indent=4)
            print('-' * 80)

            data_response_count  = 1
    print("Done with while loop")

async def main():
    _ = asyncio.create_task(td_stream_client.start_pipeline())
    
    coro = asyncio.to_thread(TDA_Streaming.loop_test)
    await coro

asyncio.run(main())

The idea of the program is to have a background task that streams data from an API to my program. While this is happening, I want to be able to do other things. Maybe have manual input...maybe have a GUI where I can interact with things.

The issue is that my program terminates as soon as the master thread finishes. How do I prevent this from happening? If I have a while loop with an "input" call, this input blocks the program. What is the best way to proceed?

CodePudding user response:

Please wait for the background task explicitly:

def loop_test():
    print("task is running")
    time.sleep(2)
    print("task is finished")

async def start_pipeline(self):

    print("Starting TD Stream")

    # Build data pipeline
    await self.td_stream_client.build_pipeline()

    data_response_count = 0
    self.streaming = True

    # Keep going while receiving data
    while self.streaming:
        print("Streaming")
        data = await self.td_stream_client.start_pipeline()

        # Parse if data inside
        if 'data' in data:
            content = data['data'][0]['content']

            print("Key: {}".format(content[0]['key']))
            pprint.pprint(content, indent=4)
            print('-' * 80)

            data_response_count  = 1
    print("Done with while loop")

async def main():
    background_task = asyncio.create_task(td_stream_client.start_pipeline())
    
    coro = asyncio.to_thread(TDA_Streaming.loop_test)
    await coro

    await background_task

asyncio.run(main())
  • Related