Homunculus Reticulli
Homunculus Reticulli

Reputation: 68466

Writing a python script that dosen't hog resources

I have a script which does a lot of computational work and is very resource intensive. When I am running the script (which typically takes several hours to complete), I am almost unable to use my machine, because it grinds to a halt.

I remember in the old days of VB programming, there was a yield() statement, which forced a memory hogging routine to be nice and hand over some CPU cycles to other processes.

My question is, is there a similar construct in Python which allows me to write scripts that play nicely with other processes on my machine?

Typical script below ....

# import required libs

if __name__ == '__main__':
    init()
    do_some_expensive_calcs()  # need to periodically 'yield' to other processes here - how do I do it?

Upvotes: 1

Views: 144

Answers (2)

anotherdave
anotherdave

Reputation: 6764

I think you can also do this from within the script itself with the OS package:

import os
os.nice(100)

Upvotes: 1

steffen
steffen

Reputation: 8968

If you are on a *ix machine, start your program nicely:

nice ./prog

You can also "renice" the program while it is running, e.g. with top.

The other strategy which I always worth thinking about is to improve the algorithm.

Upvotes: 0

Related Questions