📜  在Python中创建线程的不同方式

📅  最后修改于: 2022-05-13 01:55:01.975000             🧑  作者: Mango

在Python中创建线程的不同方式

线程是进程中可以调度执行的实体此外,它是可以在操作系统中执行的最小处理单元。

创建线程有多种方式:

1)不使用显式函数创建线程:

通过导入模块并单独创建Thread类对象,我们可以轻松地在hread创建。它是一种面向函数的创建线程的方式。

Python3
# Import required modules
from threading import *    
  
  
# Explicit function
def display() :                
  for i in range(10) :
    print("Child Thread")
  
  
# Driver Code    
      
# Create object of thread class    
Thread_obj = Thread(target=display)        
  
# Executing child thread
Thread_obj.start()            
  
# Executing main thread
for i in range(10):            
  print('Main Thread')


Python3
# Import required module
from threading import *
  
  
# Extending Thread class
class Mythread(Thread):
  
    # Target function for thread
    def run(self):
        for i in range(10):
            print('Child Thread')
  
  
# Driver Code
              
# Creating thread class object
t = Mythread()
  
# Execution of target function
t.start()
  
# Executed by main thread
for i in range(10):
    print('Main Thread')


Python3
# Import required modules
from threading import *
  
  
# Creating class
class Gfg:
  
    # Creating instance method
    def method1(self):
        for i in range(10):
            print('Child Thread')
  
  
# Driver Code
              
# Creating object of Gfg class
obj = Gfg()
  
# Creating object of thread by
# targeting instance method of Gfg class
thread_obj = Thread(target=obj.method1)
  
# Call the target function of threa
thread_obj.start()
  
# for loop executed by main thread
for i in range(10):
    print('Main Thread')


Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread  

在上面的例子中,我们创建了一个显式函数display() ,它打印子线程10 次。然后我们使用线程模块创建了一个名为Thread_objThread类对象。第一个线程display()方法为目标,即display()方法将由该Thread对象执行,主线程(第二个)以for循环为目标,将负责由主线程执行 10 次

注意:这里哪个线程将首先获得机会(主线程子线程)取决于Python虚拟机 (PVM) 中存在的线程调度程序,因此我们无法预测输出。  

2)通过扩展线程类创建线程:  

在这个方法中,我们将从线程模块中扩展线程类。这种创建线程的方式也称为面向对象方式。

蟒蛇3

# Import required module
from threading import *
  
  
# Extending Thread class
class Mythread(Thread):
  
    # Target function for thread
    def run(self):
        for i in range(10):
            print('Child Thread')
  
  
# Driver Code
              
# Creating thread class object
t = Mythread()
  
# Execution of target function
t.start()
  
# Executed by main thread
for i in range(10):
    print('Main Thread')
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread

在上面的例子中,我们创建了一个扩展Thread类的类,在这个类中,我们必须覆盖run()方法,该方法将成为我们子线程的目标函数,当start()方法被调用时,它会启动执行线程run()方法(目标函数)。

3.创建线程而不扩展线程类:  

另一种创建Thread的面向对象方式是不扩展任何线程类来创建 Thread。

蟒蛇3

# Import required modules
from threading import *
  
  
# Creating class
class Gfg:
  
    # Creating instance method
    def method1(self):
        for i in range(10):
            print('Child Thread')
  
  
# Driver Code
              
# Creating object of Gfg class
obj = Gfg()
  
# Creating object of thread by
# targeting instance method of Gfg class
thread_obj = Thread(target=obj.method1)
  
# Call the target function of threa
thread_obj.start()
  
# for loop executed by main thread
for i in range(10):
    print('Main Thread')
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread

在上面的程序中,我们分别创建了一个线程类和Gfg类的对象,每次创建一个线程类的对象时,我们也不得不提到目标函数。线程类对象以Gfg 类的实例方法为目标。要开始执行目标函数,我们必须调用start()方法。