2
0
mirror of https://gitlab.com/ita1024/waf.git synced 2025-01-11 19:05:04 +01:00

Python is getting a bit better at threading, we do not need to recycle threads anymore

This commit is contained in:
Thomas Nagy 2012-04-09 23:17:31 +02:00
parent 062c5cae1a
commit 5448e23e40

View File

@ -7,7 +7,7 @@ Runner.py: Task scheduling and execution
""" """
import random, atexit import random
try: try:
from queue import Queue from queue import Queue
except ImportError: except ImportError:
@ -25,9 +25,9 @@ class TaskConsumer(Utils.threading.Thread):
They wait for tasks in the queue and then use ``task.process(...)`` They wait for tasks in the queue and then use ``task.process(...)``
""" """
def __init__(self): def __init__(self, q=None):
Utils.threading.Thread.__init__(self) Utils.threading.Thread.__init__(self)
self.ready = Queue() self.ready = q or Queue()
""" """
Obtain :py:class:`waflib.Task.TaskBase` instances from this queue. Obtain :py:class:`waflib.Task.TaskBase` instances from this queue.
""" """
@ -54,46 +54,6 @@ class TaskConsumer(Utils.threading.Thread):
tsk(self) tsk(self)
else: else:
tsk.process() tsk.process()
pool = Queue()
"""
Pool of task consumer objects
"""
def get_pool():
"""
Obtain a task consumer from :py:attr:`waflib.Runner.pool`.
Do not forget to put it back by using :py:func:`waflib.Runner.put_pool`
and reset properly (original waiting queue).
:rtype: :py:class:`waflib.Runner.TaskConsumer`
"""
try:
return pool.get(False)
except Exception:
return TaskConsumer()
def put_pool(x):
"""
Return a task consumer to the thread pool :py:attr:`waflib.Runner.pool`
:param x: task consumer object
:type x: :py:class:`waflib.Runner.TaskConsumer`
"""
pool.put(x)
def _free_resources():
global pool
lst = []
while pool.qsize():
lst.append(pool.get())
for x in lst:
x.ready.put(None)
for x in lst:
x.join()
pool = None
atexit.register(_free_resources)
class Parallel(object): class Parallel(object):
""" """
Schedule the tasks obtained from the build context for execution. Schedule the tasks obtained from the build context for execution.
@ -253,30 +213,21 @@ class Parallel(object):
def init_task_pool(self): def init_task_pool(self):
# lazy creation, and set a common pool for all task consumers # lazy creation, and set a common pool for all task consumers
pool = self.pool = [get_pool() for i in range(self.numjobs)]
self.ready = Queue(0) self.ready = Queue(0)
def setq(consumer): pool = self.pool = [TaskConsumer(self.ready) for i in range(self.numjobs)]
consumer.ready = self.ready
for x in pool:
x.ready.put(setq)
return pool return pool
def free_task_pool(self): def free_task_pool(self):
# return the consumers, setting a different queue for each of them # free the task pool, if any
def setq(consumer):
consumer.ready = Queue(0)
self.out.put(self)
try: try:
pool = self.pool pool = self.pool
except AttributeError: except AttributeError:
pass pass
else: else:
for x in pool: for x in self.pool:
self.ready.put(setq) self.ready.put(None)
for x in pool: for x in self.pool:
self.get_out() x.join()
for x in pool:
put_pool(x)
self.pool = [] self.pool = []
def start(self): def start(self):
@ -356,6 +307,5 @@ class Parallel(object):
#print loop #print loop
assert (self.count == 0 or self.stop) assert (self.count == 0 or self.stop)
# free the task pool, if any
self.free_task_pool() self.free_task_pool()