Skip to content

run and shutdown a python module in daemon or in frontground using ssh(available on linux and windows) 使用ssh远程运行或关闭python模块,后台运行。[Linux 和 Windows通用]

License

Notifications You must be signed in to change notification settings

sric0880/pyRPCInDaemon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyRPCInDaemon

Install

pip install .

Usage

These are more use cases in tests folder.

TCP server based task

import rpcindaemon

task_id = 1000
cmd = "python heavy_task.py --arg-live-time=40"
port = 9999 # optional
t = rpcindaemon.Task(
    task_id,
    cmd,
    ssh_config["hostname"],
    username=ssh_config["user"],
    password=ssh_config["pwd"],
    port=port,
)

in heavy_task.py

import fire
import rpcindaemon

class CustomServerCmd(rpcindaemon.ServerCmd):
    def add(self, arg1, arg2=1):
        return arg1 + arg2

@rpcindaemon.makedaemon(server_cmd=CustomServerCmd)
def heavy_backgournd_task(task_id: int, f: rpcindaemon.F, arg_live_time=20):
    t = 0
    while True:
        if t > arg_live_time:
            break
        time.sleep(0.5)
        t += 0.5

if __name__ == "__main__":
    fire.Fire(heavy_backgournd_task)

then you can send do add from remote task

assert t.do_rpc("add", 1, 2) == 3
assert t.do_rpc("add", 1) == 2

or you can generate multiple processes in daemon task

import time
import rpcindaemon
import multiprocessing

def child_process(a, is_daemon):
    msg_queue = rpcindaemon.daemonize.message_queue
    lock = rpcindaemon.daemonize.lock
    with lock:
        msg_queue.put(f"{a}: send message to parent process")
        time.sleep(1)

@rpcindaemon.makedaemon()
def heavy_multiprocess_task(task_id: int, f: rpcindaemon.F, max_cpus=None):
    def msg_handler(msg):
        print(msg)
    args = list(range(20))
    f.run_parallel(
        child_process,
        args,
        max_cpus=max_cpus,
        lock=multiprocessing.Lock(),
        message_queue=multiprocessing.JoinableQueue(),
        message_handler=msg_handler,
    )

if __name__ == "__main__":
    fire.Fire(heavy_multiprocess_task)

you can terminate remote daemon task(process) whenever you want. you can setup your own signal handlers for quiting gracefully.

t.terminate()

Third-party library

  • daemoniker with a little modification of the source code

About

run and shutdown a python module in daemon or in frontground using ssh(available on linux and windows) 使用ssh远程运行或关闭python模块,后台运行。[Linux 和 Windows通用]

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages