Skip to content

Commit 20009cd

Browse files
committed
stop thread loop using Event from threading
1 parent 8d8abc3 commit 20009cd

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

opensips/event/datagram.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ def create(self):
4141
else:
4242
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
4343
self.sock.bind(self.sock_name)
44+
self.sock.setblocking(False)
4445

4546
def handle(self, callback, stop):
46-
while not stop:
47-
data = self.sock.recv(1024)
48-
callback(data)
49-
47+
while not stop.is_set():
48+
try:
49+
data = self.sock.recv(1024)
50+
callback(data)
51+
except BlockingIOError:
52+
pass

opensips/event/subscriber.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from ..mi import MI
2121
from .datagram import Datagram
2222
from .stream import Stream
23-
from threading import Thread
23+
from threading import Thread, Event
2424

2525
class EventInterface():
2626
def __init__(self, mi: MI, type: str, **kwargs):
@@ -43,13 +43,14 @@ def subscribe(self, event: str, callback, expire=3600):
4343
raise Exception("Failed to subscribe to event")
4444

4545
self.socket.create()
46-
self.thread_stop = False
46+
self.thread_stop = Event()
47+
self.thread_stop.clear()
4748
self.thread = Thread(target=self.socket.handle, args=(callback, self.thread_stop))
4849
self.thread.start()
4950

5051
except Exception as e:
5152
raise e
5253

5354
def stop(self):
54-
self.thread_stop = True
55+
self.thread_stop.set()
5556
self.thread.join()

0 commit comments

Comments
 (0)