-
Notifications
You must be signed in to change notification settings - Fork 0
/
mq_worker.py
75 lines (61 loc) · 2.14 KB
/
mq_worker.py
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
69
70
71
72
73
74
75
# Built-ins
import time
# IPC-related # TODO: fix up imports here
import ipcqueue.posixmq
from ipcqueue.serializers import RawSerializer
from ipcqueue import posixmq
# Qt related #
from PySide6.QtCore import QObject, Signal, Slot, QRunnable
class WorkerSignals(QObject):
"""
Defines the signals available from a running worker thread.
Supported signals are:
finished
No data
error
tuple (exctype, value, traceback.format_exc() )
result
object data returned from processing, anything
"""
finished = Signal() # QtCore.Signal
error = Signal(tuple)
result = Signal(object)
# noinspection PyUnresolvedReferences
class Worker(QRunnable):
"""
Worker thread for the BXT message queue
"""
def __init__(self, *args, **kwargs):
super(Worker, self).__init__()
# Store constructor arguments (re-used for processing)
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
self.should_stop = False
def stop_queue(self, stop_or_not) -> None:
self.should_stop = stop_or_not
@Slot() # QtCore.Slot
def run(self) -> None:
"""
Worker thread for the Message Queue
"""
try:
q1 = posixmq.Queue("/BunnymodXT-BunnySplit", serializer=RawSerializer, maxsize=8192, maxmsgsize=8192)
except ipcqueue.posixmq.QueueError:
print("Couldn't open the message queue. Is a BXT game running?")
return
while True:
if self.should_stop:
# TODO: Is this ↓ useful? Are we going to stop the MQ manually any other time than on quitting the app?
self.should_stop = not self.should_stop
break
if q1.qsize() > 0:
try:
msg = q1.get_nowait()
# bsp.parse_message(msg)
self.signals.result.emit(msg)
except posixmq.QueueError as e:
self.signals.error.emit(e) # TODO: as the docstring says
print("Exception")
else:
time.sleep(0.01) # CPU usage, be gone