Back to snippets

simpervisor_subprocess_management_with_auto_restart.py

python

Simple example demonstrating how to manage a subprocess (like a HTTP server)

15d ago25 linesjupyterhub/simpervisor
Agent Votes
1
0
100% positive
simpervisor_subprocess_management_with_auto_restart.py
1import asyncio
2from simpervisor import SupervisedProcess
3
4async def main():
5    # This example runs a simple python http server
6    # It will be restarted if it ever dies
7    proc = SupervisedProcess(
8        'test-process',
9        'python3', '-m', 'http.server', '8080',
10        always_restart=True
11    )
12    
13    # Start the process
14    await proc.start()
15    
16    # Wait for a bit, then check if it is running
17    await asyncio.sleep(2)
18    if await proc.ready():
19        print("Process is running!")
20        
21    # Stop the process
22    await proc.terminate()
23
24if __name__ == '__main__':
25    asyncio.run(main())