【问题标题】:Running Python Threads Simotaneously同时运行 Python 线程
【发布时间】:2022-06-24 00:11:28
【问题描述】:

我正在考虑运行一些代码以每 X 分钟自动保存一次游戏,但它也有一个接受键盘输入的线程。这是我试图同时运行的一些示例代码,但它们似乎一个接一个地运行。如何让它们同时运行?

import time
import threading

def countdown(length,delay):
    length += 1
    while length > 0:
        time.sleep(delay)
        length -= 1
        print(length, end=" ")

countdown_thread = threading.Thread(target=countdown(3,2))
countdown_thread2 = threading.Thread(target=countdown(3,1))
countdown_thread.start()
countdown_thread2.start()

【问题讨论】:

    标签: python


    【解决方案1】:

    当你创建线程时,它们应该被创建为

    Thread(target=countdown, args=(3,2))
    

    按原样运行countdown(3,2),并将结果作为线程目标传递!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 2013-08-13
      相关资源
      最近更新 更多