0

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?

2
  • 1
    You can wait for the background task explicitly after your main foreground job is done. Commented Jan 26, 2023 at 10:03
  • @AndrewSvetlov How is that done? Commented Jan 27, 2023 at 20:55

1 Answer 1

1

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()) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.