python-3.x – Is it possible to run two loops at the same time in python?

Question:

I was wanting to make a kind of game where you buy some things that help you profit, and you would have an amount you earn per second, but I don't know if I can run two loops (one to make the interaction with the player and the other to add an amount of money to the player per second) in python, if anyone knows, can you help me?

Answer:

You can use threads. Using it allows you to parallelize tasks in your program. If you use this solution, if there is any variable that is modified by more than one thread, I recommend that you create a lock. The reason is that, without a lock, the variable can take on a different value than desired.

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

https://docs.python.org/3/library/threading.html#lock-objects

Scroll to Top