Setting process title at web2py startup

63 views
Skip to first unread message

Michael Ellis

unread,
Aug 9, 2016, 11:48:10 AM8/9/16
to web2py-users

I'm running web2py in conjunction with a suite of other, independent, python processes.  In development I often want to 'ps' or kill the entire suite from the command line.  I've found the Python setproctitle module really useful for prepending a common label to all the process titles.

In each process's __main__ code, I use something like the following:

from setproctitle import setproctitle, getproctitle
_proclabel
= "thisapp"
if not getproctitle().startswith(_proclabel):
    setproctitle
(_proclabel + '  ' + getproctitle())



This puts the label at the front, so I can do things like 


ps ax | grep 'thisapp' 

17162 s000  S      0:00.15 thisapp processes  

17163 s000  S      0:00.02 thisapp simulation_service  

17164 s000  S      0:00.06 thisapp statehouse_service  

17167 s000  S      0:00.08 thisapp serial_interface_service  

17168 s000  S      0:00.01 thisapp email_service  

17169 s000  S      0:00.05 thisapp periodic_serial_query_service  


I want to also label the web2py process at startup.  So far, the only way I've found is to include the setproctitle code in an app's  0.py model file and then have my startup script fetch the index page after web2py is up and running.  


It would be nicer to be able to put it in script that runs once as web2py.py starts, but there doesn't seem to be a way to do that without the -R option which is recommended on for -S shell invocations of web2py.  


I'd rather not hack web2py.py or one of the gluon modules.  Is there a better way?


Thanks!

Massimo Di Pierro

unread,
Aug 16, 2016, 7:55:18 PM8/16/16
to web2py-users
Not clear what you mean by what web2py starts. depending on the web server there may be more than one process and the web server manages those. My guess is that you want it done at the level of the web2py server rocket. In which case I would copy web2py.py into main.py and edit the latter.

Michael Ellis

unread,
Aug 17, 2016, 4:08:08 PM8/17/16
to web2py-users
Thanks, Massimo.  I'm using web2py as an interface to an industrial process controller.  The default rocket server and sqlite3 db are fine for this app as it will never be serving pages to more than 2 or 3 clients at time (usually just one via localhost).  The process I want to label needs to be the same one your startup message advises to kill with SIGTERM.  That's the one that runs the model and controller on each page request, right?  

Your suggestion to paste web2py.py into the body of my top-level process is intriguing.  

Looking at the code in web2py.py and considering that 1) my code will never run as a frozen exe on Windows and 2) I can ignore at least for now the coverage import, it appears I could reduce it to:

import os

import sys

path = os.path.dirname(os.path.abspath(__file__))  # alter to make sure we point toplevel web2py dir.

os.chdir(path)

sys.path = [path] + [p for p in sys.path if not p == path]

# important that this import is after the os.chdir

import gluon.widget

# Start Web2py (without cron service) !

#TODO fix up sys.args to match my use case

#TODO call setproctitle here    

gluon.widget.start(cron=True)

So if I wrap all the above in a function,  would I be able to launch it successfully with multiprocessing.Process? That would be a real help as I could then dispense with a top-level shell script that's now launching web2py separately from my other processes.   And would the setproctitle() call give the desired result or would I need to push that down into gluon.widget?

Thanks!





--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/zdVUnKgm1zQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Massimo Di Pierro

unread,
Aug 18, 2016, 3:42:31 PM8/18/16
to web2py-users
I think what you propose should work fine. I would set cron=False btw.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.

Michael Ellis

unread,
Aug 18, 2016, 4:42:13 PM8/18/16
to web2py-users
Thanks.  I'll let you know how it works out.

To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/zdVUnKgm1zQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages