Python 简明教程

Python - Starting a Thread

在 Python 中,启动线程涉及使用 threading 模块中的 Thread 类提供的 start() 方法。此方法启动线程的活动,并在单独的执行线程中自动调用其 run() 方法。这意味着,当您对每个线程对象(例如,thread1、thread2、thread3)调用 start() 以启动它们的执行时。

In Python, starting a thread involves using the start() method provided by the Thread class in the threading module. This method initiates the thread’s activity and automatically calls its run() method in a separate thread of execution. Meaning that, when you call start() on each thread object (for example., thread1, thread2, thread3) to initiate their execution.

Python 以并发执行在每个 Thread 实例中定义的 run() 方法启动单独的线程。主线程在启动子线程后继续执行。

Python to launch separate threads that concurrently execute the run() method defined in each Thread instance. And the main thread continues its execution after starting the child threads.

在本教程中,您将看到有关如何在多线程编程中有效使用 start() 方法的详细解释和示例,以了解其在多线程应用程序中的行为。

In this tutorial, you will see a detailed explanation and example of how to use the start() method effectively in multi-threaded programming to understand its behavior in multi-thread applications.

Starting a Thread in Python

start() 方法是开始执行线程的基础。它设置了线程的环境并调度它运行。重要的是,它只应在每个 Thread 对象上调用一次。如果对同一个 Thread 对象多次调用此方法,它将引发 RuntimeError。

The start() method is fundamental for beginning the execution of a thread. It sets up the thread’s environment and schedules it to run. Importantly, it should only be called once per Thread object. If this method is called more than once on the same Thread object, it will raise a RuntimeError.

以下是使用 start() 方法在 Thread 对象上的语法 −

Here is the syntax for using the start() method on a Thread object −

threading.thread.start()

Example

让我们看下面的示例,展示如何在 Python 中使用 start() 方法启动新线程。

let’s see the below example, that demonstrates how to start a new thread in Python using the start() method.

from threading import Thread
from time import sleep

def my_function(arg):
   for i in range(arg):
      print("child Thread running", i)
      sleep(0.5)
thread = Thread(target = my_function, args = (10, ))
thread.start()
print("thread finished...exiting")

执行上述代码后,将生成以下结果 −

When the above code is executed, it produces the following result −

child Thread running 0
thread finished...exiting
child Thread running 1
child Thread running 2
child Thread running 3
child Thread running 4
child Thread running 5
child Thread running 6
child Thread running 7
child Thread running 8
child Thread running 9

Example

这里还有另一个演示 start() 方法工作原理的示例。您可以观察到,如果不针对线程 2 调用 start() 方法,它将保持非活动状态并且不会开始执行。

Here is another example demonstrating the working of the start() method. You can observe that, by not calling the start() method on thread2, it remains inactive and does not begin execution.

import threading
import time

class MyThread(threading.Thread):
   def __init__(self, threadID, name, counter):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name
      self.counter = counter

   def run(self):
      print("Starting " + self.name)
      print_time(self.name, self.counter)
      print("Exiting " + self.name)

def print_time(threadName, counter):
   while counter:
      time.sleep(1)
      print("%s: %s" % (threadName, time.ctime(time.time())))
      counter -= 1

# Create new threads
thread1 = MyThread(1, "Thread-1", 1)
thread2 = MyThread(2, "Thread-2", 2)
thread3 = MyThread(3, "Thread-3", 3)

# Start new Threads
thread1.start()
thread3.start()

print("Exiting Main Thread")

以上代码将生成以下输出 −

The above code will produce the following output −

Starting Thread-1
Starting Thread-3
Exiting Main Thread
Thread-1: Mon Jun 24 18:24:59 2024
Exiting Thread-1
Thread-3: Mon Jun 24 18:24:59 2024
Thread-3: Mon Jun 24 18:25:00 2024
Thread-3: Mon Jun 24 18:25:01 2024
Exiting Thread-3