How to limit maximum CPU usage for python program

I searched it over the Internet, but did not find anything related to it.

I have two large python dictionaries that contain more than two million key pairs. My computer constantly shows 100% usage when I do any calculations on this data.

In this regard, I can not perform another task in my system, because it hangs often.

Is there a way to limit the maximum CPU allocation for a python program by writing code in the python program itself. Since I do not want this program to use 100% processor time.

PS: I am currently using the sleep function to limit it, but it looks stupid. I am using windows 7.

+4
source share
1 answer

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 completely released. If you are waiting for disk I / O on a disk / swap, for example, nice really will not 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

+5
source

Source: https://habr.com/ru/post/1483060/


All Articles