python函数的并行执行
我的要求是在将控制权交给“thread\u func”之后,而循环应该在不等待“thread\u func”完成的情况下继续 请建议我如何接近python函数的并行执行,python,multithreading,Python,Multithreading,我的要求是在将控制权交给“thread\u func”之后,而循环应该在不等待“thread\u func”完成的情况下继续 请建议我如何接近 def thread_func(mySeries): time.sleep(30) print("first value is:: ", mySeries.iloc[0]) print("high value is:: ", mySeries.max()) print("low value is:: ", mySeries
def thread_func(mySeries):
time.sleep(30)
print("first value is:: ", mySeries.iloc[0])
print("high value is:: ", mySeries.max())
print("low value is:: ", mySeries.min())
print("last value is:: ", mySeries.iloc[-1])
print("=" * 20)
def testfunc():
while True:
data = myfunc(loop_stop_flag,1)
mySeries = Series(data)
my_thread = Thread(target=thread_func(mySeries))
my_thread.daemon = True # Daemonize thread
my_thread.start() # Start the execution
stop_flag = False
为线程方面创建了一个非常简单的类。没有复制mySeries,但是应该很容易适应
from threading import Thread
from time import sleep
class thread_func (Thread):
def __init__(self):
Thread.__init__(self)
self.first = 'First'
def run(self):
sleep(5)
print("first value is:: ", self.first)
print("high value is:: ", self.first)
print("low value is:: ", self.first)
print("last value is:: ", self.first)
print("=" * 20)
if __name__ == '__main__':
my_thread = thread_func()
my_thread.daemon = False # Daemonize thread
my_thread.start() # Start the execution
print('Carry on')
sleep(3)
my_thread2 = thread_func()
my_thread2.daemon = False # Daemonize thread
my_thread2.start() # Start the execution
print('Three seconds in')
sleep(3)
print('End of main')
以下一行:
my_thread = Thread(target=thread_func(mySeries))
在调用thread
的构造函数之前计算thread\u func(mySeries)
——这是因为它试图将thread\u func
的结果作为目标传递
target
参数应传递给函数对象,因此正确的构造如下所示:
my_thread = Thread(target=thread_func, args=(mySeries,))
这个my_thread=thread(target=thread\u func(mySeries))应该是my_thread=thread(target=thread\u func,args=mySeries))