Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
kill a thread
#8
(Apr-10-2022, 08:43 PM)Skaperen Wrote: exiting this script will then reset the network so the pings work again. the purpose of the script is to quickly detect when a remote host can no longer be reached. the idea is N missing ping replies means it is down. then the invoking script resets it and does this, again.
In your other Thread talk about ping,and icmplib has a async_ping.
Would think that this work fine for this task.
To write some more code and make a fake server error(on our server) after 60-sec,then wait 10-sec and do a sever/ping restart.
Run also schedule threaded and async_ping(the name give it way).
from icmplib import async_ping
import asyncio
import time, os
import schedule
import threading

def server_down():
    print('Server is down restart after 10-sec')
    job = schedule.every(10).seconds.do(run_threaded, check_run1)

async def is_alive(address, count):
    host = await async_ping(address, count, interval=0.2, timeout=4)
    if host.packets_received == 4:
        print(f'{host.address} is down!')
        server_down()
    else:
        print(f'{host.address} is up!')

def check_run():
    return asyncio.run(is_alive('python-forum.io', count=4))

def check_run1():
    return asyncio.run(is_alive('python-forum.io', count=0))

def run_threaded(job_func):
    job_thread = threading.Thread(target=job_func)
    job_thread.start()

if __name__ == '__main__':
    check_run()
    schedule.every(60).seconds.do(run_threaded, check_run)
    while True:
        schedule.run_pending()
        time.sleep(1)
Output:
104.21.27.41 is down! Server is down restart after 10-sec 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is down! Server is down restart after 10-sec 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up! 104.21.27.41 is up!
Quote:maybe, if i get the process ID number, i could kill the process.
If want a example of this look this post or post.
psutil is great tool for this.
And of course don't want to check contiguous that use Cpu time,so then use it a schedule way.
Gribouillis likes this post
Reply


Messages In This Thread
kill a thread - by Skaperen - Apr-10-2022, 07:17 PM
RE: kill a thread - by Gribouillis - Apr-10-2022, 07:37 PM
RE: kill a thread - by Skaperen - Apr-10-2022, 08:43 PM
RE: kill a thread - by Skaperen - Apr-10-2022, 08:46 PM
RE: kill a thread - by Gribouillis - Apr-10-2022, 09:57 PM
RE: kill a thread - by Skaperen - Apr-11-2022, 03:25 AM
RE: kill a thread - by Gribouillis - Apr-11-2022, 09:02 AM
RE: kill a thread - by snippsat - Apr-11-2022, 02:02 PM
RE: kill a thread - by Skaperen - Apr-14-2022, 01:32 AM
what's going on - by Skaperen - Apr-14-2022, 02:14 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020