简体   繁体   English

将kwargs传递给线程运行器方法的最佳方法

[英]Best way to pass kwargs to a thread runner method

I'm developing a threaded data collector that queries storage devices. 我正在开发查询存储设备的线程数据收集器。

I have the following thread runner method: 我有以下线程运行器方法:

def threadrunner(cfg, filer, APPLIANCES, kwargs):

    _client  = login(cfg, filer)
    _filer   = DeviceConfig(_client, Version=True)
    APPLIANCES.append(_filer)

And the code that calls it: 以及调用它的代码:

    newthr = threading.Thread(target=threadrunner, args=(cfg, appliance, APPLIANCES, kwargs))
    newthr.name = appliance
    newthr.start()

What is the best way to pass a variable list of **kwargs to threadrunner()? 将** kwargs变量列表传递给threadrunner()的最佳方法是什么? In the example above Version=True is one version of a kwarg I need to pass. 在上面的示例中,Version = True是我需要传递的kwarg的一个版本。 All kwargs that I need to pass are simple True|False toggles. 我需要传递的所有kwarg都是简单的True | False切换。

You can pass the kwargs keyword 您可以传递kwargs关键字

You could do something like this: 您可以执行以下操作:

def threadrunner(*args, **kwargs):

    _client  = login(*args)
    if 'APPLIANCES' not in kwargs:
        raise ValueError('Appliances not in kwargs')
    APPLIANCES = kwargs.pop('APPLIANCES', None)

    #Assuming your boolean values go here.
    _filer   = DeviceConfig(_client, **kwargs)
    APPLIANCES.append(_filer)

newthr = threading.Thread(target=threadrunner, args=(cfg, appliance,) kwargs={'APPLIANCES': APPLIANCES, 'boolkey1':boolval1, ....)
newthr.name = appliance
newthr.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM