summaryrefslogtreecommitdiff
path: root/regtest/InterruptibleQueue.py
blob: 4156e30b45ecc99654c03f23ff80ac2e1e0feb04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# InterruptibleQueue.py
#
# Copyright (C) 2016 Carlos Garcia Campos <carlosgc@gnome.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from threading import Lock, Condition
from collections import deque
import sys

class InterruptibleQueue:
    """Simpler implementation of Queue that uses wait with a timeout to make join interruptile"""

    def __init__(self):
        self._queue = deque()
        self._mutex = Lock()
        self._finished_condition = Condition(self._mutex)
        self._not_empty_condition = Condition(self._mutex)
        self._n_unfinished_tasks = 0

    def task_done(self):
        self._finished_condition.acquire()
        try:
            n_unfinished = self._n_unfinished_tasks - 1
            if n_unfinished == 0:
                self._finished_condition.notify_all()
            self._n_unfinished_tasks = n_unfinished
        finally:
            self._finished_condition.release()

    def join(self):
        self._finished_condition.acquire()
        try:
            while self._n_unfinished_tasks:
                self._finished_condition.wait(sys.float_info.max)
        finally:
            self._finished_condition.release()

    def put(self, item):
        self._mutex.acquire()
        try:
            self._queue.append(item)
            self._n_unfinished_tasks += 1
            self._not_empty_condition.notify()
        finally:
            self._mutex.release()

    def get(self):
        self._not_empty_condition.acquire()
        try:
            while not len(self._queue):
                self._not_empty_condition.wait()
            return self._queue.popleft()
        finally:
            self._not_empty_condition.release()