【发布时间】:2020-07-07 22:26:34
【问题描述】:
我有一种情况,我必须调用两种不同的方法来并行运行。我正在使用 Python 线程模块来实现这一点。但是,它们不是并行运行的两种方法,而是顺序运行。有人可以指导我了解我的代码有什么问题吗?
这适用于 Python 3.5,使用线程模块并有一个具有两种不同方法的类,必须并行运行。
## This is in template.py
from threading import Thread
import time
class createTemplate:
def __init__(self,PARAM1):
self.PARAM1=PARAM1
def method1(self):
print("Method1-START")
time.sleep(120)
print("Method1-END")
def method2(self):
print("Method2-START")
time.sleep(120)
print("Method2-END")
def final_method(self):
if self.PARAM1=="1":
m1=Thread(target=self.method1)
m1.run()
if self.PARAM1=="1":
m2=Thread(target=self.method2)
m2.run()
## This is in createTemplate.py
from template import createTemplate
template = createTemplate("1")
template.final_method()
实际输出:
方法1-开始 方法1-END 方法2-开始 方法2-END
预期输出:
方法1-开始 方法2-开始 方法1-END 方法2-END
【问题讨论】:
标签: python python-3.x multithreading python-multithreading