worker: add threading lock
add lock to avoid same proxy to be scanned multiple time when a small number a jobs is handed to worker
This commit is contained in:
@@ -149,15 +149,18 @@ class WorkerThread():
|
|||||||
self.thread = None
|
self.thread = None
|
||||||
self.workqueue = []
|
self.workqueue = []
|
||||||
self.workdone = []
|
self.workdone = []
|
||||||
|
self.lock = threading.Lock()
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.done.set()
|
self.done.set()
|
||||||
def term(self):
|
def term(self):
|
||||||
if self.thread: self.thread.join()
|
if self.thread: self.thread.join()
|
||||||
def add_jobs(self, jobs):
|
def add_jobs(self, jobs):
|
||||||
self.workqueue.extend(jobs)
|
with self.lock:
|
||||||
|
self.workqueue.extend(jobs)
|
||||||
def return_jobs(self):
|
def return_jobs(self):
|
||||||
jobs = self.workqueue
|
with self.lock:
|
||||||
self.workqueue = []
|
jobs = self.workqueue
|
||||||
|
self.workqueue = []
|
||||||
return jobs
|
return jobs
|
||||||
def jobcount(self):
|
def jobcount(self):
|
||||||
return len(self.workqueue)
|
return len(self.workqueue)
|
||||||
@@ -168,14 +171,21 @@ class WorkerThread():
|
|||||||
def start_thread(self):
|
def start_thread(self):
|
||||||
self.thread = threading.Thread(target=self.workloop)
|
self.thread = threading.Thread(target=self.workloop)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
def pop_if_possible(self):
|
||||||
|
with self.lock:
|
||||||
|
if len(self.workqueue):
|
||||||
|
job = self.workqueue.pop()
|
||||||
|
else:
|
||||||
|
job = None
|
||||||
|
return job
|
||||||
def workloop(self):
|
def workloop(self):
|
||||||
success_count = 0
|
success_count = 0
|
||||||
job_count = 0
|
job_count = 0
|
||||||
duration_total = 0
|
duration_total = 0
|
||||||
duration_success_total = 0
|
duration_success_total = 0
|
||||||
while True:
|
while True:
|
||||||
if len(self.workqueue):
|
job = self.pop_if_possible()
|
||||||
job = self.workqueue.pop()
|
if job:
|
||||||
nao = time.time()
|
nao = time.time()
|
||||||
job.run()
|
job.run()
|
||||||
spent = time.time() - nao
|
spent = time.time() - nao
|
||||||
|
|||||||
Reference in New Issue
Block a user