简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM