Search code examples
pythonpandasdataframepython-multithreading

passing a dataframe to a thread


Inside a function, I created a local dataframe with name resamp_df. I am trying to pass this local dataframe to a thread function as an argument for running some algorithm on it. Here is my code:

main function

if readyForOrder:
    order_thread = threading.Thread(target=order_management, name='thread1', args=resamp_df)
    order_thread.start()

thread function

def order_management(df):
    global  readyForOrder, order_id, order_id_counter, ltp
    if df.shape[0] >= 3:
         readyForOrder = False
         old_ltp = df.iat[-2, 0]
         new_ltp = df.iat[-1, 0]

But my thread is not running. It generates following errors:

TypeError: order_management() takes 1 positional argument but 7 were given

Any suggestions to make it work?

Thanks in advance


Solution

  • Pass in arguments as a tuple

    args=(resamp_df, )
    

    as the documentation mentions args as a tuple.

    Args is a tuple because the task function takes multiple positional arguments and you need to have an appropriate data structure to hold that.