Infinite While True Loop in the Background (Python)

一曲冷凌霜 提交于 2021-02-19 00:53:07

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/38254172/infinite-while-true-loop-in-the-background-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!