summaryrefslogtreecommitdiff
path: root/service/app/reactor_manager.py
blob: 01f7f5450f179a34b28367dc0d82992462db6e7c (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
import signal
import sys
from threading import Thread
from twisted.internet import reactor


def signal_handler(signal, frame):
        stop_reactor_on_exit()
        sys.exit(0)


def start_reactor():
    def start_reactor_run():
        reactor.run(False)

    global REACTOR_THREAD
    REACTOR_THREAD = Thread(target=start_reactor_run)
    REACTOR_THREAD.start()


def stop_reactor_on_exit():
    reactor.callFromThread(reactor.stop)
    global REACTOR_THREAD
    REACTOR_THREAD = None

signal.signal(signal.SIGINT, signal_handler)