无限而背景中的真循环(Python)

时间:2016-07-07 19:52:57

标签: python multithreading background-process

基本上,我有一些像这样的代码:

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

在此之前,我有一个启动屏幕。然而,由于这是真正的循环,它阻止它运行实际的启动屏幕。相反,它只是显示这个。

那么如何将代码放在后台呢?

1 个答案:

答案 0 :(得分:6)

尝试多线程。

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()
相关问题