Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite While True Loop in the Background (Python)

basically, I have some code like this:

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

In front of this I have a start up screen. However because of this while true loop, it blocks it from running the actual start up screen. Instead, it just displays this.

So how do you put the code in the background?

like image 248
Ross McPherson Avatar asked Jul 07 '16 19:07

Ross McPherson


1 Answers

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()
like image 147
Eli Sadoff Avatar answered Nov 07 '22 14:11

Eli Sadoff