【问题标题】:Python concurrency - t1.start() TypeError: 'int' object is not callablePython 并发 - t1.start() TypeError: 'int' object is not callable
【发布时间】:2018-07-23 06:19:05
【问题描述】:

我最近在一次技术面试中遇到了这个问题:

打印系列010203040506。使用多线程,第一个线程只打印0,第二个线程只打印偶数,第三个线程只打印奇数。

虽然我有一些 Python 经验,但我从未真正编写过任何多线程代码。因此,在阅读了一些文档之后,我设法创建了可以完成这项工作的类。我正在尝试将事情放在一起,但不确定如何去做。有人可以为此提供基于锁或信号量的解决方案吗?

  import threading


class PrintSeries(threading.Thread):
    def __init__(self, start, stop, step, string):
        threading.Thread.__init__(self)
        self.string = string
        self.start = start
        self.stop = stop
        self.step = step

    def run(self):
        if self.start < self.stop:
            self.start += self.step
        self.string += str(self.start)
s = ''
t1 = PrintSeries(0, 0, 0, s)
t2 = PrintSeries(1, 2, 5, s)
t3 = PrintSeries(2, 2, 6, s)

t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
print(s)

无论如何,即使这样也会遇到以下错误,

t1.start() TypeError: 'int' object is not callable

【问题讨论】:

    标签: python multithreading concurrency multiprocessing


    【解决方案1】:

    这是使用condition 对象的解决方案

    使用条件变量的典型编程风格使用锁 同步访问某些共享状态;线程是 对特定的状态变化感兴趣,反复调用 wait() 直到他们看到所需的状态,而修改状态的线程 当他们以这种方式改变状态时调用 notify() 或 notifyAll() 这可能是其中一位服务员想要的状态

    预期的执行顺序是 (t1, t3, t1, t2, t1, t3, ...) 所以我们只需创建一个状态变量来保存当前状态,每个线程只有在满足预期条件时才会工作。之后它会更新状态并唤醒其他线程

    from threading import Thread, Lock, Condition
    
    
    def zero_printer(max_iters):
        global state
        for _ in range(max_iters):
            with cv:
                while state not in (0, 2):
                    cv.wait()
                state += 1
                print('0')
                cv.notify_all()
    
    
    def even_printer(max_iters):
        global state
        c = 2
        for _ in range(max_iters):
            with cv:
                while state != 3:
                    cv.wait()
                print(c)
                state = 0
                cv.notify_all()
            c += 2
    
    
    def odd_printer(max_iters):
        global state
        c = 1
        for _ in range(max_iters):
            with cv:
                while state != 1:
                    cv.wait()
                print(c)
                state = 2
                cv.notify_all()
            c += 2
    
    
    iters = 3
    state = 0
    cv = Condition(Lock())
    
    t1 = Thread(target=zero_printer, args=(iters * 2,))
    t2 = Thread(target=even_printer, args=(iters,))
    t3 = Thread(target=odd_printer, args=(iters,))
    
    t1.start()
    t2.start()
    t3.start()
    
    t1.join()
    t2.join()
    t3.join()
    

    结果:

    0
    1
    0
    2
    0
    3
    0
    4
    0
    5
    0
    6
    

    我没有费心把它合并成一行。

    您的代码中的错误是您误用了 threading.Thread constructor 即传递一个整数值作为期望 target 参数,因此它试图执行您的 int em> 值并产生相应的错误消息

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 2017-04-06
      • 2013-03-30
      • 2019-11-11
      • 1970-01-01
      • 2022-08-23
      • 2015-01-05
      • 2014-11-25
      相关资源
      最近更新 更多