Infinite though True Loop in the background (Python)

basically, I have code like this:

while True:
   number = int(len(oilrigs)) * 49
   number += money
   time.sleep(1)

Before that, I have a start screen. However, because of this, while the true loop, it blocks it from starting the actual initial screen. Instead, it simply displays this.

So how do you put the code in the background?

+4
source share
1 answer

Try multithreading.

import threading

def background():
    while True:
        number = int(len(oilrigs)) * 49
        number += money
        time.sleep(1)

def foreground():
    # What you want to run in the foreground

b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)

b.start()
f.start()
+5
source

All Articles