我的要求是在将控制权交给“thread_func”之后,而循环应继续进行,而不必等待“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_func(mySeries)的构造函数之前先评估Thread,这是因为它试图将thread_func的结果作为target传递。

应该将target参数传递给一个函数对象,因此正确的构造应如下所示:
    my_thread = Thread(target=thread_func, args=(mySeries,))

09-25 21:40