Python Thread : TypeError: __main__.generate_num() argument after * must be an iterable, not Event

huangapple go评论82阅读模式
英文:

Python Thread : TypeError: __main__.generate_num() argument after * must be an iterable, not Event

问题

from threading import Thread, Event
import queue
import random

def generate_num(e):
    print('e', e.is_set())
    while True:
        if e.is_set():
            print('g', random.randint(15, 20))
        else:
            print('g', 0)

def start_generating():
    i = 0
    e = Event()
    r_thread = Thread(target=generate_num, args=(e,), daemon=True).start()  # Fix the args tuple
    print('starting')

    while True:
        print('i', i)
        if i % 5 == 0:
            e.set()
        if i == 12:
            exit(0)
        i += 1

start_generating()

错误信息表明在调用generate_num函数时,参数应该是可迭代的,但传递了一个Event对象。为了修复这个问题,你需要将参数传递给generate_num函数时放入一个元组中,就像上面的代码示例中修复的那样。

英文:

I am experiment the Threads with Event

from threading import Thread, Event
import queue
import random

def generate_num(e):
    print('e',e.is_set())
    while True:
        if e.is_set():
            print('g', random.randint(15, 20))
        else:
            print('g', 0)
    
def start_generating():
    i = 0
    e = Event()
    r_thread = Thread(target=generate_num, args=(e), daemon=True).start()
    print('starting')

    while True:
        print('i', i)
        if i %5 == 0:
            e.set()
        if i ==12:
            exit(0)
        i+=1

start_generating()

My aim is that when i becomes 5 or 10, it should print the random number, otherwise it should print 0.

But, I got this error

starting
i 0
i 1
i 2
Exception in thread Thread-6 (generate_num):
Traceback (most recent call last):
i 3
i 4
i 5
i 6
i 7
i 8
i 9
File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    i 10
iself.run()
File "/usr/lib/python3.10/threading.py", line 953, in run
11
    self._target(*self._args, **self._kwargs)
TypeError: __main__.generate_num() argument after * must be an iterable, not Event
i 12

How to solve this, thanks

答案1

得分: 0

如评论中指出并且在错误中也提到,args 不是一个可迭代对象,更具体地说,它应该是一个元组,这通常是 args 参数应该的形式。

创建只包含一个元素的元组需要在元素后面加上逗号 ,

one_element_tuple = (1,)

所以只需这样做:

from threading import Thread, Event
import queue
import random

def generate_num(e):
    print('e', e.is_set())
    while True:
        if e.is_set():
            print('g', random.randint(15, 20))
        else:
            print('g', 0)
    
def start_generating():
    i = 0
    e = Event()
    r_thread = Thread(target=generate_num, args=(e,), daemon=True).start()
    print('starting')

    while True:
        print('i', i)
        if i % 5 == 0:
            e.set()
        if i == 12:
            exit(0)
        i += 1

start_generating()
英文:

As pointed out in the comments and also in the error the args are not an iterable or to be more specific a tuple, which is what the args argument normally should be.

Creating a tuple with only one element needs a trailing ,

one_element_tuple = (1,)

So just do

from threading import Thread, Event
import queue
import random

def generate_num(e):
    print('e',e.is_set())
    while True:
        if e.is_set():
            print('g', random.randint(15, 20))
        else:
            print('g', 0)
    
def start_generating():
    i = 0
    e = Event()
    r_thread = Thread(target=generate_num, args=(e,), daemon=True).start()
    print('starting')

    while True:
        print('i', i)
        if i %5 == 0:
            e.set()
        if i ==12:
            exit(0)
        i+=1

start_generating()

huangapple
  • 本文由 发表于 2023年7月13日 14:12:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定