Here's another attempt at putting code in a groups post.

@button leo-ver-serv-start:

@language python
'''Start 'leo-ver-serv' for History Tracer plugin'''
import os
watchfiles = os.path.join(g.computeHomeDir(), '.leo/.leoRecentFiles.txt')

if os.path.exists(watchfiles):
    g.es_print(f"Running: leo-ver-serv {watchfiles} 8088")
    os.spawnlp(os.P_NOWAIT, 'leo-ver-serv', watchfiles, '8088')
else:
    g.es_print(f"Couldn't find {watchfiles}")

'''TODO: see if can use psutils instead for better communication/monitoring
https://psutil.readthedocs.io/en/latest/#popen-class'''

@button leo-ver-serv-list

@language python
g.es_print("====", c.p.h, "====")
g.es_print("Looking for 'leo-ver-serv' processes...")
import psutil

process = [proc for proc in psutil.process_iter() if proc.name() == 
"leo-ver-serv"]
# g.es_print(f"Process: {process}") # debug

g.es_print("{:>6}   {:<20}   {:<10}".format('PID', 'Name', 'Status'))

for p in process:
    g.es_print("{:>6}   {:<20}   {:<10}".format(p.pid, p.name(), p.status
()))
    # g.es_print(p.open_files()) # raises AccessDenied if not run as root
    #g.es_print(p.children()) # not needed, always empty (so far)
  
g.es_print("=" * 40)


@button leo-ver-serv-stop:

@language python
'''Gracefully shutdown any processes named 'leo-ver-serv'
Adapted from https://psutil.readthedocs.io/en/latest/#psutil.wait_procs
'''
import psutil

def on_terminate(proc):
    print("process {} terminated with exit code {}".format(proc, proc.
returncode))

procs = [proc for proc in psutil.process_iter() if proc.name() == 
"leo-ver-serv"]

for p in procs:
    p.terminate()
gone, alive = psutil.wait_procs(procs, timeout=3, callback=on_terminate)
for p in alive:
    p.kill()


-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/295e28c4-3e8e-4f50-bbdb-fc52f62d84f0%40googlegroups.com.

Reply via email to