我正在Windows平台上使用Python 3。我使用
问题在于在调用
我需要这个参数一直保持为列表。如何做到这点?
我在文档中读到
提前感谢您的时间!
以下是 MWE:
threading.Thread
动态运行函数,并且可能会带或不带参数进行调用。我正在设置一个包含多个项的列表,其中第一项是定义路径的字符串。其他参数将是列表中后面的项。因此,args可能等于['C:\SomePath']
,也可能等于['C:\SomePath', 'First Argument', 'Second Argument']
。我的调用看起来像这样:my_script = threading.Thread(target=scr_runner, args=q_data.data)
my_script.start()
问题在于在调用
threading.Thread
和/或 start
函数的过程中,参数正在失去其列表特征(isinstance(q_data.data, str)=False
),但在接受 script_to_run_data
参数的 scr_runner
函数内部,isinstance(script_to_run_data, str)=True.
。我需要这个参数一直保持为列表。如何做到这点?
我在文档中读到
threading.Thread
函数期望一个元组作为参数。将类似 ['C:\SomePath']
这样的内容转换为元组时,会出现某些问题,导致结果变成了字符串吗?提前感谢您的时间!
以下是 MWE:
# coding=utf-8
""" This code tests conversion to list in dynamic calling. """
import threading
def scr_runner(script_to_run_data: tuple) -> None:
""" This is the function to call dynamically. """
is_list = not isinstance(script_to_run_data, str)
print("scr_runner arguments are a list: T/F. " + str(is_list))
my_list=['C:\SomePath']
is_list = not isinstance(my_list, str)
print("About to run script with list argument: T/F. " + str(is_list))
my_script = threading.Thread(target=scr_runner, args=my_list)
my_script.start()
现在奇怪的是,当我使my_list具有更多的元素时,我会收到一个错误:
# coding=utf-8
""" This code tests conversion to list in dynamic calling. """
import threading
def scr_runner(script_to_run_data: tuple) -> None:
""" This is the function to call dynamically. """
is_list = not isinstance(script_to_run_data, str)
print("scr_runner arguments are a list: T/F. " + str(is_list))
my_list=['C:\SomePath', 'First Argument', 'Second Argument']
is_list = not isinstance(my_list, str)
print("About to run script with list argument: T/F. " + str(is_list))
my_script = threading.Thread(target=scr_runner, args=my_list)
my_script.start()
产生错误:
About to run script with list argument: T/F. True
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 916, in
_bootstrap_inner
self.run()
File "C:\ProgramData\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: scr_runner() takes 1 positional argument but 3 were given
args=my_list
更改为args=[my_list]
,尽管我认为这种行为有点违反直觉。 - Adrian Keisterargs
没有被隐式解包为target
的参数,你如何期望将两个或更多参数传递给线程? - ShadowRanger