类型错误:创建自定义线程池时“bool"对象不可调用 [英] TypeError: 'bool' object is not callable while creating custom thread pool

查看:121
本文介绍了类型错误:创建自定义线程池时“bool"对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义线程池,以便更好地控制代码以满足未来的需求.到目前为止,我还没有能够写出一些功能性的东西.我希望线程与主解释器进程分开工作.我不需要多核优势.Threads(Worker) 应该监听队列大小的变化并在传递的作业上运行执行.我无法使此代码工作.有没有人看到任何解决方案来完成这项工作?

I want to create a custom thread pool for having more control over the code for future needs. So far I haven't been able to write something functional. I want the threads to work separately from the main interpreter process. I don't need multi-core advantage. Threads(Worker) should listen for a change in queue size and run execute on the passed job. I cannot make this code work. Does anybody see any solution to make this work?

import queue
from threading import Thread
import time
import random

class Worker(Thread):
#----------------------------------------------------------
    def __init__(self,queue,x):
        Thread.__init__(self)
        self.run = True
        self.queue = queue
        self.x = x
#----------------------------------------------------------
    def run(self):
        while self.run:
            while not self.queue.empty():
                job = self.queue.get()
                print("Starting", job, self.x)
                job.execute()
                time.sleep(0.1)
                self.queue.task_done()
            time.sleep(0.1)

class TestJob:
    def __init__(self,x):
        self.x = x

    def execute(self):
        print(f"Num {self.x}")

class DownloadManager:
    def __init__(self,numOfThread):
        self.jobQueue = queue.Queue()
        self.numOfThread = numOfThread
        self.threadList = [Worker(self.jobQueue, x) for x in range(0, self.numOfThread)]
        [x.start() for x in self.threadList]
        print("End of init")

    def addJob(self,job):
        self.jobQueue.put(job)

dm = DownloadManager(2)

for x in range(0,10):
    job = TestJob(x)
    dm.addJob(job)

print("After adding all jobs")
input("Waiting for enter")
print("Done")

控制台输出

Exception in thread Thread-1:
End of init
Traceback (most recent call last):
After adding all jobs
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
TypeError: 'bool' object is not callable

Waiting for enterException in thread Thread-2:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
TypeError: 'bool' object is not callable



Done

我的 Python 版本是 3.7.2.

推荐答案

你的 Worker 类有 2 个 run 属性.

Your Worker class has 2 attributes for run.

class Worker(Thread):
#----------------------------------------------------------
    def __init__(self,queue,x):
        ...
        self.run = True       
        ...
#----------------------------------------------------------
    def run(self):            
        while self.run:
            ...

一个是布尔值(self.run = True),一个是函数(def run(self):).

One is a boolean (self.run = True) and one is a function (def run(self):).

您不能同时拥有同名的方法和属性.

根据错误消息,self.run() 正在被调用,因此 run 应该是一个函数.尝试将属性 self.run 更改为其他名称(例如 self.is_running).

Based on the error message, self.run() is being called, so run is expected be a function. Try changing the property self.run to some other name (ex. self.is_running).

这篇关于类型错误:创建自定义线程池时“bool"对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆