Answer the question
In order to leave comments, you need to log in
Why doesn't threading work with class?
Hello!
Rewrote monitoring script using recently learned classes. For some reason multithreading fell off. Please tell me what is the problem
import requests, datetime, time, os, json
from platform import system
from settings import *
def worktime():
hour = datetime.datetime.now().strftime("%H")
if "08" < hour < "23":
return True
def system_path():
if system() == 'Windows':
system_path = 'C:/pymon_logs/'
else:
system_path = '/var/log/pymon/'
if not os.path.exists(system_path):
os.makedirs(system_path)
return system_path
class Monitor:
def __init__(self, service, *request_params):
self.request_params = request_params
self.service = service
self.silence = 0
self.night_await = 0
self.night_raised = 0
self.req_status = ''
self.path = system_path()
def request(self):
try:
if len(self.request_params) == 1:
self.req_status = requests.get(self.request_params[0])
else:
self.req_status = requests.post(self.request_params[0],
headers=self.request_params[1],
data=json.dumps(self.request_params[2]))
if self.req_status.ok:
self.night_raised = 0
return self.req_status.status_code
except Exception as error:
self.req_status = error
return self.req_status
def log(self):
now = round(time.time())
today = str(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"))
if not worktime():
if self.night_raised == 0:
self.night_await = now + 180
self.night_raised = 1
if now >= self.silence:
if not worktime():
bot.send_message(chat_id, f'{self.service} is Down')
self.silence = now + 120
else:
if now >= self.night_await:
bot.send_message(chat_id, f'{self.service} is Down')
self.night_raised = 0
with open(f"{self.path}{self.service.lower()}_log.txt", "a") as file:
file.write(f'{today} {self.service.upper()}: \n {self.req_status} \n\n')
run = True
def start_mon(service):
while 1:
while run:
value = service.request()
if value != 200:
service.log()
time.sleep(2)
else:
continue
def start_cabinet():
cabinet = Monitor('Cabinet', cabinetUrl)
start_mon(cabinet)
def start_site():
site = Monitor('sitename', site_url)
start_mon(site)
def start_back():
back = Monitor('Back', back_url, back_headers, back_data)
start_mon(back)
def start_auth_solo():
auth_solo = Monitor('Auth', auth_url, auth_headers, auth_data)
start_mon(auth_solo)
def start_api():
api = Monitor('API', api_url)
start_mon(api)
import threading
from logic import *
if __name__ == "__main__":
threads = list()
auth = threading.Thread(target=start_mon(start_auth_solo))
threads.append(auth)
auth.start()
backend = threading.Thread(target=(start_back))
threads.append(backend)
backend.start()
cabinet = threading.Thread(target=start_mon(start_cabinet))
threads.append(cabinet)
cabinet.start()
site = threading.Thread(target=start_mon(start_sit))
threads.append(site)
site.start()
''' IF START POLLING WITH PRODUCTION RUNNING
IT WILL CRASH THE PRODUCTION MONITORING '''
bot = threading.Thread(target=bot.polling)
threads.append(bot)
bot.start()
api = threading.Thread(target=start_mon(start_api))
threads.append(api)
api.start()
Answer the question
In order to leave comments, you need to log in
So:
auth = threading.Thread(target=start_mon(start_auth_solo))
auth = threading.Thread(target=start_mon, args=(start_auth_solo,))
from time import sleep
from threading import Thread
def a(duration):
while True:
print('Я функция а')
sleep(duration)
def b(duration):
while 1:
print('Я функция b')
sleep(duration)
def c(duration):
while 1:
print('Я функция c')
sleep(duration)
def main():
ta = Thread(target=a, args=(1, ))
ta.start()
tb = Thread(target=b, args=(2, ))
tb.start()
tc = Thread(target=c, args=(3, ))
tc.start()
while True:
print('Я главный поток!')
sleep(5)
if __name__ == '__main__':
main()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question