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

Ros*_*son 5 python multithreading background-process

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

while True:
   number = int(len(oilrigs)) * 49
   number += money
   time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

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

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

Eli*_*off 7

尝试多线程.

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()
Run Code Online (Sandbox Code Playgroud)