Update main.py

Go multithreaded.
This commit is contained in:
Leigh Phillips 2021-01-20 15:54:19 -08:00 committed by GitHub
parent 85505c3087
commit add7337b1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 7 deletions

24
main.py
View File

@ -5,6 +5,7 @@ import datetime
import subprocess import subprocess
from pythonping import ping from pythonping import ping
from influxdb import InfluxDBClient from influxdb import InfluxDBClient
from multiprocessing import Process
# InfluxDB Settings # InfluxDB Settings
NAMESPACE = os.getenv('NAMESPACE', 'None') NAMESPACE = os.getenv('NAMESPACE', 'None')
@ -191,8 +192,8 @@ def pingtest():
'target' : target 'target' : target
}, },
'fields': { 'fields': {
'success' : int(pingtest.success()), 'success' : int(pingtest._responses[0].error_message is None),
'rtt': pingtest.rtt_avg_ms 'rtt': float(0 if pingtest._responses[0].error_message is not None else pingtest.rtt_avg_ms)
} }
} }
] ]
@ -202,15 +203,24 @@ def pingtest():
print("Ping Failed.") print("Ping Failed.")
def main(): def main():
pPing = Process(target=pingtest)
pSpeed = Process(target=speedtest)
init_db() # Setup the database if it does not already exist. init_db() # Setup the database if it does not already exist.
loopcount = 1 loopcount = 0
while (1): # Run a Speedtest and send the results to influxDB indefinitely. while (1): # Run a Speedtest and send the results to influxDB indefinitely.
if loopcount % PING_INTERVAL == 0: if loopcount == 0 or loopcount % PING_INTERVAL == 0:
pingtest() if pPing.is_alive():
pPing.terminate()
pPing = Process(target=pingtest)
pPing.start()
if loopcount % TEST_INTERVAL == 0: if loopcount == 0 or loopcount % TEST_INTERVAL == 0:
speedtest() if pSpeed.is_alive():
pSpeed.terminate()
pSpeed = Process(target=speedtest)
pSpeed.start()
if loopcount % ( PING_INTERVAL * TEST_INTERVAL ) == 0: if loopcount % ( PING_INTERVAL * TEST_INTERVAL ) == 0:
loopcount = 0 loopcount = 0