-
Notifications
You must be signed in to change notification settings - Fork 8
/
daemon.py
47 lines (41 loc) · 1.26 KB
/
daemon.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
""" Threads to run constantly """
import time
from threading import Thread
from youtube import Youtube
from stocks import Portfolio
def daily_thread():
""" Print daily data every minute """
youtube = Youtube()
portfolio = Portfolio()
while True:
try:
youtube.subscribers()
portfolio.value()
except ConnectionError:
time.sleep(300)
time.sleep(60)
def youtube_thread():
""" Print Youtube Subs Every minute """
yt_instance = Youtube()
while True:
try:
yt_instance.subscribers()
except ConnectionError:
time.sleep(300) # Sleep if error
time.sleep(60)
class Daemon():
""" Run an arbitrary number of threads in the background of the bot """
def __init__(self):
self.threadlist = [daily_thread]
self.active_threads = []
self.start_threads()
def start_threads(self):
""" Start list of threads """
for thread in self.threadlist:
active_thread = Thread(target=thread)
active_thread.start()
self.active_threads.append(active_thread)
def end_threads(self):
""" End all active threads """
for thread in self.active_threads:
thread.exit()