Python Threading问题:TypeError in Threading. function takes 1 positional argument but 100 were given

在使用python多线程module Threading时:

import threading
t = threading.Thread(target=getTemperature, args = (id1))
t.start()

运行时报如上的错误,参考stackoverflow,如下解释:

The args kwarg of threading.Thread expects an iterable, and each element in that iterable is being passed to the target function.
Since you are providing a string for args:
  t = threading.Thread(target=startSuggestworker, args = (start_keyword))
each character is being passed as a separate argument to startSuggestworker.
Instead, you should provide args a tuple:
  t = threading.Thread(target=startSuggestworker, args = (start_keyword,))
也就是args传递的参数类型不对,即使一个参数也要时元组的形式给出

正确的传递方式如下:

import threading
t = threading.Thread(target=getTemperature, args = (id1,))
t.start()

原文地址:https://www.cnblogs.com/yyxianren/p/10774113.html

时间: 2024-10-12 15:16:12

Python Threading问题:TypeError in Threading. function takes 1 positional argument but 100 were given的相关文章

Python:XXX missing X required positional argument: 'self'

代码的一个小小Bug有时候会让人焦头烂额,费了很大劲搞明白的问题,如果不记录下来,是很容易遗忘的! 定义一个类,如果按照以下的方式使用,则会出现TypeError: testFunc() missing 1 required positional argument: 'self'.如果认真细究的话,笔者曾反复修改参数,但是于事无补. 在Python中,应该先对类进行实例化,然后在应用类,如下图所示.注意,实例化的过程是应该待括号的. 总结:Python中,类应该先实例化,然后再使用类! Pyth

[python]多线程模块thread与threading

Python通过两个标准库(thread, threading)提供了对多线程的支持 thread模块 import time import thread def runner(arg): for i in range(6): print str(i)+':'+arg time.sleep(1) #结束当前线程 thread.exit_thread() #等同于thread.exit() #启动一个线程,第一个参数为函数名, #第二个参数为一个tuple类型,是传给函数的参数 thread.st

python 报错TypeError: 'range' object does not support item assignment,解决方法

贴问题 nums = range(5)#range is a built-in function that creates a list of integers print(nums)#prints "[0,1,2,3,4]" print(nums[2:4])#Get a slice from index 2 to 4 (exclusive); prints '[2,3]" print(nums[2:])#Get a slice from index 2 to the end

TypeError: translate() takes exactly one argument (2 given)

python3.6下使用translate(None, string.punctuation)去除句子中的标点符号,报错:TypeError: translate() takes exactly one argument (2 given) 原因是python版本的问题,python2下该语句正常执行,python3中translate的参数只有一个,正确做法: sentence='The girl is a painter, and her sisiter is a dancer.' tran

python问题:TypeError: a bytes-like object is required, not 'str'

源程序: import socket target_host = "www.baidu.com" # 127.0.0.1 target_port = 80 # 建立一个socket对象 client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 创建TCP连接 # 连接客户端 client.connect((target_host,target_port)) client.send("GET / HTTP/1.1\r

Python错误:TypeError:'str' does not support the buffer interface

在socket套接字模块进行send和recv方法时出现这种问题,是因为Python3.x和Python2.x版本变化,In python 3, bytes strings and unicodestrings are now two different types. 相互之间需要进行转换decode()和encode(). send()需要的参数为bytes类型,因此需要对str进行encode() recv()返回的是bytes类型,因此我们需要对返回的bytes进行decode()转换为s

python 3.5: TypeError: a bytes-like object is required, not 'str'

出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了str()函数,就会出现该错误,该错误不会再python2中出现. 具体解决方法有以下两种: 第一种,在open()函数中使用'r'属性,即文本方式读取,而不是'rb',以二进制文件方式读取,可以直接解决问题. 第二种,在open()函数中使用'rb',可以在使用之前进行转换,有以下实例,来自:htt

执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: 'on_delete' 解决方法: 在连接外键时加上: on_delete=models.CASCADE 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument

Python TypeError: get_logger() missing 1 required positional argument: 'self'

Python 调用类时,提示缺少self参数 调用类方法时,需要加上一个小括号,修改为:logger = Logger().get_logger()后,运行正常,没有报错. 加了一个小括号之后,表示我们队该类进行了实例化了. Python TypeError: get_logger() missing 1 required positional argument: 'self' 原文地址:https://www.cnblogs.com/ch-10/p/10331198.html