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:
mickael
2019-01-13 16:41:48 +00:00
parent f489f0c4dd
commit aaac14d34e

View File

@@ -149,15 +149,18 @@ class WorkerThread():
self.thread = None
self.workqueue = []
self.workdone = []
self.lock = threading.Lock()
def stop(self):
self.done.set()
def term(self):
if self.thread: self.thread.join()
def add_jobs(self, jobs):
self.workqueue.extend(jobs)
with self.lock:
self.workqueue.extend(jobs)
def return_jobs(self):
jobs = self.workqueue
self.workqueue = []
with self.lock:
jobs = self.workqueue
self.workqueue = []
return jobs
def jobcount(self):
return len(self.workqueue)
@@ -168,14 +171,21 @@ class WorkerThread():
def start_thread(self):
self.thread = threading.Thread(target=self.workloop)
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):
success_count = 0
job_count = 0
duration_total = 0
duration_success_total = 0
while True:
if len(self.workqueue):
job = self.workqueue.pop()
job = self.pop_if_possible()
if job:
nao = time.time()
job.run()
spent = time.time() - nao