-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy paththread_manager.py
57 lines (45 loc) · 1.43 KB
/
thread_manager.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
import os
import time
from threading import Thread
from coin_data import CoinData
from signals_loop import MainLoop, scheduler
from trader import Trader
class ThreadManager:
def __init__(self):
"""Construct all required instances and start threads"""
self.trader = Trader()
self.data = CoinData()
self.signals = MainLoop(self.data)
self.threads = []
self.start_thread(self.jobs)
print('Signals background tasks added')
self.start_thread(self.save_data_thread)
print('Data thread running')
def save_data_thread(self):
try:
while True:
time.sleep(2)
self.data.save_latest_data()
except KeyboardInterrupt as e:
self.teardown()
os.remove('symbols.db')
raise e
def teardown(self):
scheduler.remove_all_jobs()
self.stop_threads()
scheduler.shutdown()
self.signals.teardown()
self.data.bsm_tear_down()
def jobs(self):
jobs = [(self.trader.check_positions_cancel_open_orders, 'interval', 60)]
self.signals.start_jobs(jobs=jobs)
def start_thread(self, func):
t = Thread(target=func)
t.setDaemon(True)
t.start()
self.threads.append(t)
def stop_threads(self):
self.data.bsm_tear_down()
for thread in self.threads:
thread.join()
print('Threads stopped')