5

I have searched it over internet but found nothing related to it.

I have two large python dictionaries that contains more than 2 Million key value pairs .My computer is continuously showing 100% utilization when I am doing any kind of computation on this data.

Due to this I am not able to perform other task on my system as it hangs frequently.

Is there any way that can restrict the maximum CPU allocation for a python program by writing some code in the python program itself.As I do not want to allow this program to use 100% cpu time.

PS: I am currently using sleep function to restrict it but it looks silly.I am using windows 7.

5
  • 4
    CPU time not used is CPU time wasted; unless the problem is to avoid overheating in badly assembled computers, the solution isn't to lower the CPU utilization, but to lower the priority of your process, so if there's any other task competing for the CPU your process will always "lose", thus leaving the system responsive. Commented May 28, 2013 at 2:24
  • Also, see: stackoverflow.com/questions/1023038/… Commented May 28, 2013 at 2:27
  • @MatteoItalia That question is about unix/linux platform .As I am a windows7 user so looking something for windows .Thanks for reference . Commented May 28, 2013 at 2:30
  • Nope, that question is about a cross-platform way; scroll below, there are solutions both for Windows and for Linux. Commented May 28, 2013 at 2:34
  • In this SetPriority (p) method they have given 4 different priority for the process .Is there any doc available that can tell me that how the resource allocation has been made for these priorities. Commented May 28, 2013 at 2:43

1 Answer 1

5

If you are on a linux/unix platform you can use nice to reduce the priority of your process.

This only helps if it is cpu that is maxed out. If you are waiting on disk/swap I/O for example, nice really won't help.

NICE(1)                          User Commands                         NICE(1)

NAME
       nice - run a program with modified scheduling priority

SYNOPSIS
       nice [OPTION] [COMMAND [ARG]...]

DESCRIPTION
       Run  COMMAND  with an adjusted niceness, which affects process schedul‐
       ing.  With no COMMAND, print the current  niceness.   Nicenesses  range
       from -20 (most favorable scheduling) to 19 (least favorable).

For Windows try the START command

Sign up to request clarification or add additional context in comments.

3 Comments

Although, there's ionice to give the process lower priority in IO requests.
you can also access nice from the os module, os.nice() docs.python.org/2/library/os.html#os.nice
@JohnLaRooy Can you solve this confusion please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.