问题
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")
CONSOLE OUTPUT
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
My Python version is 3.7.2.
回答1:
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:
...
One is a boolean (self.run = True
) and one is a function (def run(self):
).
You can't have both a method and an attribute with the same name.
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
).
来源:https://stackoverflow.com/questions/56408992/typeerror-bool-object-is-not-callable-while-creating-custom-thread-pool