[Dprglist] Running a Python thread at elevated priority

Murray Altheim murray18 at altheim.com
Sat Feb 20 14:00:42 PST 2021


Hi Chris,

I just wanted to tease out one thing that would help me and potentially
anyone else using Python on a robot.

On 21/02/21 6:43 am, Chris N wrote:
[...]
> If you are not doing ALL of the following, then you are simply wasting your time:
> 
> 1. Run your application – and all its threads - at elevated priority. Not
>    only that, be sure to run your real-time threads at higher priority than 
>    your non-real-time threads.    The same would be true when one is using a  
>    RTOS. The OS can’t magically know which activity is most important.  You 
>    have to tell it. 

Can you do this from within your Python code, or is it done when you call
the Python interpreter on the command line? I'm currently starting my ros.py
(main application) up from the command line (it uses argparse internally),
or from a rosd.py systemctl daemon triggered by listening to a GPIO-wired
toggle switch on the robot, so I can turn my ROS on and off with a switch.

So I can either do something inside ros.py or inside rosd.py, since the
latter creates the command line that calls the former. If its possible
to do so inside ros.py that'd be easier and more consistent as I normally
just type "ros.py -s" to start things up.

I'm currently doing this using psutil* to set thread priority from within
the ros.py file** (line 93):

     import psutil
     ...
     proc = psutil.Process(os.getpid())
     proc.nice(10)

Is that correct? Or perhaps, is that what you're doing? Or something else?

I'm currently hoping to whittle my application down to three threads:

   1. main ros.py application
   2. system Clock
   3. gamepad (when used)
   4. ballistic behaviours (run individually then quit)

You're suggesting running all my application threads at an elevated
priority. Do you suggest running them all at the same elevated priority,
or perhaps prioritising, say, my Clock thread even higher?

Cheers,

Murray

* https://psutil.readthedocs.io/en/latest/
** https://github.com/ifurusato/ros/blob/master/ros.py
...........................................................................
Murray Altheim <murray18 at altheim dot com>                       = =  ===
http://www.altheim.com/murray/                                     ===  ===
                                                                    = =  ===
     In the evening
     The rice leaves in the garden
     Rustle in the autumn wind
     That blows through my reed hut.
            -- Minamoto no Tsunenobu



More information about the DPRGlist mailing list