首页 > 解决方案 > 如何将列表和字典传递给线程创建函数

问题描述

在以传统方式创建线程时,我想传递列表和字典对象;怎么做?

例如:

def expectslist(**kwargs):
    list=kwargs.values     -- not working

def expectsdict(**kwargs):
    dictobject=kwargs.values -- not working

def main():
     thread1 = Thread(target =expectslist,  (args=mylist)) # mylist is list of objects
     thread2 = Thread(target =expectsdict,  (args=mydict)) # mydict is dictionary of objects (key-objects pair)

标签: pythonpython-3.xmultithreading

解决方案


的值argstuple保存线程函数参数的 a。

例子:

def my_sum(x,y):
  return x + y

Thread(target = my_sum,args = (5,9))

推荐阅读