将参数传递给 Python 线程时出现类型错误
TypeError When Passing Arguments to a Python Thread
我正在尝试编写一个简单的线程程序。当我不尝试将任何变量传递给目标函数时,它运行良好。
import threading
import time
import datetime
def my_function():
thread_start_time = str(datetime.datetime.now())
thread_start_time_remove_microseconds = thread_start_time[:19]
print("\n\nThread Execution Start Time: ", thread_start_time_remove_microseconds)
print("I am being printed by mythread. Sleeping for 5 seconds. Main thread will wait till I finish")
time.sleep(5)
thread_end_time = str(datetime.datetime.now())
thread_end_time_remove_microseconds = thread_end_time[:19]
print("Thread Execution End Time: ", thread_end_time_remove_microseconds)
def main():
mythread= threading.Thread(target=my_function, name="thread1")
mythread.start()
print("\n\n","=" * 31,"I am printed by the main thread","=" * 31,"\n\n")
if __name__ == "__main__":
main()
结果为:
Thread Execution Start Time:
2018-09-09 17:02:46
=============================== I am printed by the main thread ===============================
I am being printed by mythread. Sleeping for 5 seconds. Main thread will wait till I finish
Thread Execution End Time: 2018-09-09 17:02:51
Process finished with exit code 0
但是,当我尝试将变量传递给函数时,出现以下错误:
导入线程
导入时间
导入日期时间
def my_function(one_variable):
print(one_variable)
thread_start_time = str(datetime.datetime.now())
thread_start_time_remove_microseconds = thread_start_time[:19]
print("\n\nThread Execution Start Time: ", thread_start_time_remove_microseconds)
print("I am being printed by mythread. Sleeping for 5 seconds. Main thread will wait till I finish")
time.sleep(5)
thread_end_time = str(datetime.datetime.now())
thread_end_time_remove_microseconds = thread_end_time[:19]
print("Thread Execution End Time: ", thread_end_time_remove_microseconds)
def main():
mythread= threading.Thread(target=my_function, name="thread1", args=("This is one variable from main"))
mythread.start()
print("\n\n","=" * 31,"I am printed by the main thread","=" * 31,"\n\n")
if __name__ == "__main__":
main()
结果为:
=============================== I am printed by the main thread ===============================
Exception in thread thread1:
Traceback (most recent call last):
File "C:\Users\dparvez\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\dparvez\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: my_function() takes 1 positional argument but 30 were given
Process finished with exit code 0
谁能帮我解释为什么会出现这个错误。
你需要一个逗号来使 args
成为一个元组,如下所示:
mythread= threading.Thread(
target=my_function
name="thread1",
args=("This is one variable from main",))
这是因为 threading.Thread() 期望 args 是一些 iterable 并枚举它以使所有 args 传递给您的函数。
表达式 ("string") 等于 "string" - 它是一个可迭代对象,结果为 ("s", "t", "r" , "i", "n", "g")。不是你想要的。要告诉 Python ("string") 是单元素元组而不是括号表达式,请执行 ("string",).
我正在尝试编写一个简单的线程程序。当我不尝试将任何变量传递给目标函数时,它运行良好。
import threading
import time
import datetime
def my_function():
thread_start_time = str(datetime.datetime.now())
thread_start_time_remove_microseconds = thread_start_time[:19]
print("\n\nThread Execution Start Time: ", thread_start_time_remove_microseconds)
print("I am being printed by mythread. Sleeping for 5 seconds. Main thread will wait till I finish")
time.sleep(5)
thread_end_time = str(datetime.datetime.now())
thread_end_time_remove_microseconds = thread_end_time[:19]
print("Thread Execution End Time: ", thread_end_time_remove_microseconds)
def main():
mythread= threading.Thread(target=my_function, name="thread1")
mythread.start()
print("\n\n","=" * 31,"I am printed by the main thread","=" * 31,"\n\n")
if __name__ == "__main__":
main()
结果为:
Thread Execution Start Time:
2018-09-09 17:02:46
=============================== I am printed by the main thread ===============================
I am being printed by mythread. Sleeping for 5 seconds. Main thread will wait till I finish
Thread Execution End Time: 2018-09-09 17:02:51
Process finished with exit code 0
但是,当我尝试将变量传递给函数时,出现以下错误: 导入线程 导入时间 导入日期时间
def my_function(one_variable):
print(one_variable)
thread_start_time = str(datetime.datetime.now())
thread_start_time_remove_microseconds = thread_start_time[:19]
print("\n\nThread Execution Start Time: ", thread_start_time_remove_microseconds)
print("I am being printed by mythread. Sleeping for 5 seconds. Main thread will wait till I finish")
time.sleep(5)
thread_end_time = str(datetime.datetime.now())
thread_end_time_remove_microseconds = thread_end_time[:19]
print("Thread Execution End Time: ", thread_end_time_remove_microseconds)
def main():
mythread= threading.Thread(target=my_function, name="thread1", args=("This is one variable from main"))
mythread.start()
print("\n\n","=" * 31,"I am printed by the main thread","=" * 31,"\n\n")
if __name__ == "__main__":
main()
结果为:
=============================== I am printed by the main thread ===============================
Exception in thread thread1:
Traceback (most recent call last):
File "C:\Users\dparvez\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\dparvez\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: my_function() takes 1 positional argument but 30 were given
Process finished with exit code 0
谁能帮我解释为什么会出现这个错误。
你需要一个逗号来使 args
成为一个元组,如下所示:
mythread= threading.Thread(
target=my_function
name="thread1",
args=("This is one variable from main",))
这是因为 threading.Thread() 期望 args 是一些 iterable 并枚举它以使所有 args 传递给您的函数。
表达式 ("string") 等于 "string" - 它是一个可迭代对象,结果为 ("s", "t", "r" , "i", "n", "g")。不是你想要的。要告诉 Python ("string") 是单元素元组而不是括号表达式,请执行 ("string",).