TypeError: recv_thread() argument after * must be an iterable, not socket

文章讲述了在Python中遇到的TypeError,尤其是在使用threading模块时,如何处理因将socket对象直接作为args传递给线程函数导致的argumentafter*mustbeiterable,notsocket错误,解决方案是将socket对象转换为元组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误

TypeError: recv_thread() argument after * must be an iterable, not socket的问题。

原因

这个错误是由于在创建一个线程时,将socket对象作为args参数传递给了recv_thread函数,而args参数需要一个可迭代的对象,比如元组或列表。

解决办法

可以通过在socket对象后面加一个逗号,将其转换为一个元组,来解决这个问题。代码是这样的:

def recv_thread(sock):
    while True:
        data = sock.recv(1024).decode()

thread = threading.Thread(target=recv_thread, args=(s))

可以改为:

def recv_thread(sock):
    while True:
        data = sock.recv(1024).decode()
        
thread = threading.Thread(target=recv_thread, args=(s,))

这样就可以避免TypeError: recv_thread() argument after * must be an iterable, not socket的错误了。

(1) Python Threading module Error argument after * must be an iterable, not int. multithreading - Python Threading module Error argument after * must be an iterable, not int - Stack Overflow.
(2) TypeError: send() argument after * must be an iterable, not socket in .... [Solved]TypeError: send() argument after * must be an iterable, not socket in Python.
(3) Python threading error - must be an iterable, not int. multithreading - Python threading error - must be an iterable, not int - Stack Overflow.
(4) python - TypeError: add() argument after * must be an iterable, not .... python - TypeError: add() argument after * must be an iterable, not Settings - Stack Overflow.

[/public/home/pengjy/anaconda3] >>> PREFIX=/public/home/pengjy/anaconda3 WARNING: md5sum mismatch of tar archive expected: 8a581514493c9e0a1cbd425bc1c7dd90 got: 614f6284c34f91affd38a1be2e4be076 - Unpacking payload ... Traceback (most recent call last): File "entry_point.py", line 76, in <module> File "tarfile.py", line 2024, in extractall File "tarfile.py", line 2065, in extract File "tarfile.py", line 2137, in _extract_member File "tarfile.py", line 2186, in makefile File "tarfile.py", line 249, in copyfileobj tarfile.ReadError: unexpected end of data [210095] Failed to execute script entry_point concurrent.futures.process._RemoteTraceback: ''' Traceback (most recent call last): File "concurrent/futures/process.py", line 368, in _queue_management_worker File "multiprocessing/connection.py", line 251, in recv TypeError: __init__() missing 1 required positional argument: 'msg' ''' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "entry_point.py", line 69, in <module> File "concurrent/futures/process.py", line 484, in _chain_from_iterable_of_lists File "concurrent/futures/_base.py", line 611, in result_iterator File "concurrent/futures/_base.py", line 439, in result File "concurrent/futures/_base.py", line 388, in __get_result concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending. [210105] Failed to execute script entry_point 是什么问题,如何解决?
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

telllong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值