【问题标题】:How to pass list and dictionary to thread create function如何将列表和字典传递给线程创建函数
【发布时间】:2021-10-07 12:56:41
【问题描述】:

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

例如:

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)

【问题讨论】:

  • 首先,你声明的函数都没有期望列表。

标签: python python-3.x multithreading


【解决方案1】:

args 的值是一个包含线程函数参数的tuple

例子:

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

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

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多