Home > Software design >  How to do something else while one part of the program is waiting in Python?
How to do something else while one part of the program is waiting in Python?

Time:10-25

I am trying to get weather data using OpenWeatherMap API in Python 3.9. But I can make only 60 calls/min. The moment I make all 60 calls, I'll have to put a delay for 60 seconds. Now, I want to do some other stuff during these 60 seconds, like saving the data I got into a database. What is the best way to achieve this?

Apologies if my question is not phrased correctly.

CodePudding user response:

Your best bet here would be multithreading.

Here are some good examples and tutorials:

https://www.tutorialspoint.com/python/python_multithreading.htm

https://www.geeksforgeeks.org/multithreading-python-set-1/

In essence, multithreading allows you to divide your program up into multiple threads. Each of these threads can do work individually of one another until one thread blocks another thread.

Depending on what "stuff" you want to do you would design the connection between your API thread and your "do stuff while waiting" thread differently.

  • Related