User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Task priority / niceness in python

Mon Sep 09, 2013 7:09 am

At some point in the nearish future, I need to make my quadcopter python code 'faster' so allow TCP connection processing / message parsing to control it - currently it's autonomous / hard coded and loops at about 50Hz which is acceptable for autonomous, but insufficient for remote control

One option I've considered is PyPy, the JIT compiler - lovely solution when it's available, but I suspect I can depend on that any time soon.

Another option is to use os.nice() to increase the process niceness value, and it's that which I have some questions about:

Can this work on the current running process (os.nice(os.getpid(), niceness) or would I have to spawn a thread / new process and set it's niceness? It suggests this is an increment - I'd prefer to set the process priority to real-time (although this is actually subtlely different to niceness).

Any ideas / thoughts much appreciated.

Thanks!
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Task priority / niceness in python

Mon Sep 09, 2013 7:18 am

Also found another option: psuti - https://code.google.com/p/psutil/wiki/Documentation:

Code: Select all

psutil.IOPRIO_CLASS_NONE
psutil.IOPRIO_CLASS_RT
psutil.IOPRIO_CLASS_BE
psutil.IOPRIO_CLASS_IDLE
A set of integers representing the I/O priority of a process on Linux. They can be used in conjunction with psutil.Process.get_ionice() and psutil.Process.set_ionice() to get or set process I/O priority. For further information refer to ionice command line utility or ioprio_get system call.
Anyone got experience or advice on these of what's best suited to the job in hand, or (dis)advantages of one against the other?

TIA
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: Task priority / niceness in python

Mon Sep 09, 2013 7:19 am

I don't see how manipulating the niceness value could gain speed for your app. It would do that only if the processor is busy with other processes at the same time. The gain is relative to other processes.

If you want to do it, could you just start the python interpreter process with the niceness parameters that you want? I see no use in manipulating them in real-time...

User avatar
paddyg
Posts: 2284
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Task priority / niceness in python

Mon Sep 09, 2013 11:35 am

Once you've got the profile data (I know you were digging into this) you could hive off the offending code into functions that you can compile with Cython. I've used this on the raspberry pi and it's remarkably easy to do (certainly compared with writing you own module in C). The biggest benefit comes from defining types in critical locations so the real-time checking can be binned, apart from that it's just the same python code
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Task priority / niceness in python

Mon Sep 09, 2013 12:04 pm

paddyg wrote:Once you've got the profile data (I know you were digging into this) you could hive off the offending code into functions that you can compile with Cython. I've used this on the raspberry pi and it's remarkably easy to do (certainly compared with writing you own module in C). The biggest benefit comes from defining types in critical locations so the real-time checking can be binned, apart from that it's just the same python code
Thanks Paddy, that's the perfect solution - I have no problem "type"ing the code - in fact, I'd prefer it as it routes out errors in the code where the interpreter may hide code errors through undercover type casting.

I'm not quite at the profiling stage yet, I need to prove the python code first, but once done, it definitely seems like this combination is the way for me to go.

Many thanks.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: Task priority / niceness in python

Mon Sep 09, 2013 12:27 pm

One further question, just in case you know - the time consumers are almost certainly my i2c and GPIO python code and the corresponding processing - essentially all my code does is read i2c sensors, perform a little math(s) on them, and feed them out via GPIO PWM. Both i2c and GPIO are python wrappers for 'C' code - do you know if Cython will handle that?
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
paddyg
Posts: 2284
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Task priority / niceness in python

Mon Sep 09, 2013 1:17 pm

I've not done it but I'm pretty sure it can. You might be able to just import the packages as usual, at worst you may have to dress it up a little. Here's a project using GPIO with cython

It's probably worth splitting the jobs into different threads or using some of the callback systems if those are available with the packages you're using.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”