The Python Oracle

Infinite While True Loop in the Background (Python)

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 Question
00:33 Accepted answer (Score 15)
00:57 Thank you

--

Full question
https://stackoverflow.com/questions/3825...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #multithreading #backgroundprocess

#avk47



ACCEPTED ANSWER

Score 17


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()