discovery: psutil module, and: history tracer control buttons

55 views
Skip to first unread message

Matt Wilkie

unread,
Mar 17, 2020, 1:02:26 AM3/17/20
to leo-editor
Hi All,

I've been having some fun this evening exploring a new-to-me and so-far-excellent cross platform module for controlling and monitoring computer processes from inside python: psutils (https://github.com/giampaolo/psutil). It is more than worth the price of admission (time spent learning).

Here's the result of my efforts so far, 3 scripts (which I have as buttons) that
  • launch Vitalijie's history_tracer plugin backend server
  • Look for and list the process id and status for the server if found
  • Gracefully shutdown the server
All from within Leo. :)

I've cross posted the scripts and sample output here https://gist.github.com/maphew/b46aac7687ace48a6467e2dc84ed6fbd, so that there's less chance of copy-paste errors from email should anyone want to play with these.


An edited excerpt from the console which started Leo


don't forget to launch leo-ver-serv!!!
==== @int history-tracer-port=8088 ====
Looking for 'leo-ver-serv' processes...
   PID   Name                   Status    
  3815   leo-ver-serv           sleeping  
========================================


This is just after Leo startup. The first thing I did was run the list services script. Sleeping is normal status for the server.

But let's pretend we don't know this and we attempt to start the service:


Running: leo-ver-serv /home/matt/.leo/.leoRecentFiles.txt 8088 thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 98, kind: AddrInUse, message: "Address already in use" }', src/libcore/result.rs:1188:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Uhoh. List the services again:

==== @int history-tracer-port=8088 ====
Looking for 'leo-ver-serv' processes...
   PID   Name                   Status    
  3815   leo-ver-serv           sleeping  
  5335   leo-ver-serv           zombie    
========================================


So we still have our first real servivce, and a new zombie because of the crash above.

Let's try and shut them down gracefully, and then list again:


process psutil.Process(pid=5335, status='terminated') terminated with exit code 101 process psutil.Process(pid=3815, status='terminated') terminated with exit code None ==== @int history-tracer-port=8088 ==== Looking for 'leo-ver-serv' processes... PID Name Status ========================================


The double bar immediately following 'Status' indicates no services found. In the next version we should have it say None and remove guesswork.


Now the scripts:

....nope, Google Groups keeps messing up the formatting. See the gist link above.


-matt

Edward K. Ream

unread,
Mar 17, 2020, 2:05:58 AM3/17/20
to leo-editor
On Tue, Mar 17, 2020 at 12:02 AM Matt Wilkie <map...@gmail.com> wrote:


I've been having some fun this evening exploring a new-to-me and so-far-excellent cross platform module for controlling and monitoring computer processes from inside python: psutils (https://github.com/giampaolo/psutil).

Thanks for this link. I've bookmarked it.

Edward

Edward K. Ream

unread,
Mar 17, 2020, 2:10:06 AM3/17/20
to leo-editor
On Tue, Mar 17, 2020 at 12:02 AM Matt Wilkie <map...@gmail.com> wrote:

Now the scripts:

....nope, Google Groups keeps messing up the formatting. See the gist link above.

Google groups has excellent formatting capabilities:

- Paste your script in the editor.
- Select it.
- Just above the editor, click the {} icon.

Edward

Matt Wilkie

unread,
Mar 17, 2020, 10:00:47 AM3/17/20
to leo-editor
Google groups has excellent formatting capabilities:

- Paste your script in the editor.
- Select it.
- Just above the editor, click the {} icon.

I tried that, a couple different ways. It would add double line feeds, or do the reverse and remove all line feeds and smush everything into a single line. I think it gets confused when there is existing formatting in the message.

Edward K. Ream

unread,
Mar 17, 2020, 10:14:46 AM3/17/20
to leo-editor
You can remove all existing formatting by pasting the code into Leo, coping from Leo, and repasting back into the online editor.

Edward

Matt Wilkie

unread,
Mar 17, 2020, 10:21:28 AM3/17/20
to leo-editor
You can remove all existing formatting by pasting the code into Leo, coping from Leo, and repasting back into the online editor.

With this I'm now getting an error from Groups: "You cannot create a poste that contains no content or attachments", yet I can clearly see and edit a post with 3 code formatted blocks in it. Strange. ...there's something wierd with my machine too though: every text element I select is copied to the system clipboard, replacing what's already there. It's happening across all applications, including the file manager. Very annoying.

Matt Wilkie

unread,
Mar 17, 2020, 10:29:03 AM3/17/20
to leo-editor
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()


Matt Wilkie

unread,
Mar 17, 2020, 10:37:12 AM3/17/20
to leo-editor
there's something wierd with my machine too though: every text element I select is copied to the system clipboard, replacing what's already there. It's happening across all applications, including the file manager. Very annoying.

Aha, found and fixed. I use a clipboard manager called CopyQ - a utility that keeps a history of clipboard items. Very useful, cross platform including android. It has a preference to automatically save all mouse selections that I must have turned on and forgotton about. (There must be another way to interact with that feature besides standard ctrl-v and middle mouse button. Otherwise I can't see how it's useful.) Anyway, disabled for me!

-matt

Edward K. Ream

unread,
Mar 17, 2020, 11:00:51 AM3/17/20
to leo-editor


On Tue, Mar 17, 2020 at 9:37 AM Matt Wilkie <map...@gmail.com> wrote:

> Aha, found and fixed. I use a clipboard manager called CopyQ - a utility that keeps a history of clipboard items. Very useful, cross platform including android. It has a preference to automatically save all mouse selections that I must have turned on and forgotten about. (There must be another way to interact with that feature besides standard ctrl-v and middle mouse button. Otherwise I can't see how it's useful.) Anyway, disabled for me!

Glad you found it.

Edward
Reply all
Reply to author
Forward
0 new messages