Skip to main content
 首页 » 编程设计

python之并行执行python函数

2024年12月31日8shangdawei

我的要求是在将控制权交给“thread_func”之后,while 循环应该继续而不等待“thread_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.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 

请您参考如下方法:

下面一行:

    my_thread = Thread(target=thread_func(mySeries)) 

在调用 Thread 的构造函数之前评估 thread_func(mySeries) - 这是因为它试图传递 thread_func 的结果 作为目标。

target 参数应该被传递一个函数对象,所以正确的构造应该是这样的:

    my_thread = Thread(target=thread_func, args=(mySeries,))