473,769 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cpu usage limit

mmf
Hi.

My problem:
How can I make sure that a Python process does not use more that 30% of
the CPU at any time. I only want that the process never uses more, but
I don't want the process being killed when it reaches the limit (like
it can be done with resource module).

Can you help me?

Thanks in advance.

Best regards,
Markus

Jul 19 '05 #1
9 4920
rbt

mf wrote:
Hi.

My problem:
How can I make sure that a Python process does not use more that 30% of
the CPU at any time. I only want that the process never uses more, but
I don't want the process being killed when it reaches the limit (like
it can be done with resource module).

Can you help me?

Thanks in advance.

Best regards,
Markus


Are you looping during a cpu intensive task? If so, make it sleep a bit
like this:

for x in cpu_task:
time.sleep(0.5)
do(x)
Jul 19 '05 #2
rbt <rb*@athop1.ath .vt.edu> wrote:

mf wrote:
Hi.

My problem:
How can I make sure that a Python process does not use more that 30% of
the CPU at any time. I only want that the process never uses more, but
I don't want the process being killed when it reaches the limit (like
it can be done with resource module).

Can you help me?

Thanks in advance.

Best regards,
Markus


Are you looping during a cpu intensive task? If so, make it sleep a bit
like this:

for x in cpu_task:
time.sleep(0.5)
do(x)


or like this (untested!)

finished = False
while not finished:
before = time.time()
do(x) # sets finished if all was computed
after = time.time()
delta = after-before
time.sleep(delt a*10/3.)

now the trick: do(x) can be a single piece of code, with strategically placed yield's
all over....

--
-----------------------------------------------------------
| Radovan GarabĂ*k http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls .savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
Jul 19 '05 #3
> Are you looping during a cpu intensive task? If so, make it sleep a bit
like this:

for x in cpu_task:
time.sleep(0.5)
do(x)


No, I don't use an intensive loop. I have about 1200 lines of code
inside a process - is there nothing like

xyz.setlimit(xy z.cpu, 0.30)

???

Thank.
Markus
Jul 19 '05 #4
rbt
ga************* *****@kassiopei a.juls.savba.sk wrote:
rbt <rb*@athop1.ath .vt.edu> wrote:
mf wrote:
Hi.

My problem:
How can I make sure that a Python process does not use more that 30% of
the CPU at any time. I only want that the process never uses more, but
I don't want the process being killed when it reaches the limit (like
it can be done with resource module).

Can you help me?

Thanks in advance.

Best regards,
Markus

Are you looping during a cpu intensive task? If so, make it sleep a bit
like this:

for x in cpu_task:
time.sleep(0.5)
do(x)

or like this (untested!)

finished = False
while not finished:


Why don't you just write 'while True'??? 'while not false' is like
saying 'I am not unemployed by Microsoft' instead of saying 'I am
employed by Microsoft'. It's confusing, complex and unnecessary. Lawyers
call it circumlocution (talking around the truth).
before = time.time()
do(x) # sets finished if all was computed
after = time.time()
delta = after-before
time.sleep(delt a*10/3.)

now the trick: do(x) can be a single piece of code, with strategically placed yield's
all over....

Jul 19 '05 #5
On 2005-05-27, ga************* *****@kassiopei a.juls.savba.sk <ga************ ******@kassiope ia.juls.savba.s k> wrote:
How can I make sure that a Python process does not use more that 30% of
the CPU at any time. I only want that the process never uses more, but
I don't want the process being killed when it reaches the limit (like
it can be done with resource module).
Are you looping during a cpu intensive task? If so, make it sleep a bit
like this:

for x in cpu_task:
time.sleep(0.5)
do(x)


or like this (untested!)

finished = False
while not finished:
before = time.time()
do(x) # sets finished if all was computed
after = time.time()
delta = after-before
time.sleep(delt a*10/3.)

now the trick: do(x) can be a single piece of code, with
strategically placed yield's all over....


Since you have no way of knowing that your process was the only
one running between the two calls to time.time(), you're
placing an upper bound on how much CPU time you're using, but
the actual usage is unknown and may be much lower on a heavily
loaded machine.

Running for 100ms and sleeping for 333ms results in an upper
limit of 25% rather than 30%. Sleeping for (delta * 7.0/3.0)
gives a 30% upper bound.

All that aside, it seems to me that this situation is analogous
to when people waste all sorts of effort trying to write clever
applications that cache parts of files or other data structures
in main memory with backing store on disk. They end up with a
big, complicated, buggy app that's slower and requires more
resources than a far simpler app that lets the OS worry about
memory management.

IOW, you're probably better off not trying to write application
code that tries to out-think your OS. Use whatever prioritizing
scheme your OS kernel provides for setting up a low priority
"background " task, and let _it_ worry about divvying up the CPU.
That's what it's there for, and it's got a far better picture
of resource availability and demand.

--
Grant Edwards grante Yow! Yow! It's a hole
at all the way to downtown
visi.com Burbank!
Jul 19 '05 #6
"mmf" <mf****@arcor.d e> writes:
How can I make sure that a Python process does not use more that 30% of
the CPU at any time. I only want that the process never uses more, but
I don't want the process being killed when it reaches the limit (like
it can be done with resource module).

Can you help me?


In general you can only do that with a real-time operating system.
Most other OS's will let you adjust process priorities so that you can
prevent your Python process from hogging cycles away from other
processes. But if the machine is idle and nobody else wants the
cycles, Python will get all of them.
Jul 19 '05 #7
rbt wrote:
ga************* *****@kassiopei a.juls.savba.sk wrote:
finished = False
while not finished:


Why don't you just write 'while True'??? 'while not false' is like
saying 'I am not unemployed by Microsoft' instead of saying 'I am
employed by Microsoft'. It's confusing, complex and unnecessary. Lawyers
call it circumlocution (talking around the truth).
before = time.time()
do(x) # sets finished if all was computed
after = time.time()
delta = after-before
time.sleep(delt a*10/3.)


The answer to your question "why not write 'while True'?" is to be found
in the helpful comment he put on the line with "do(x)".... Note that
"finished" is a flag, so "sets finished" sort of explains the whole thing.

-Peter
Jul 19 '05 #8
I understand, that what I suggest does not solve the problem you want,
but..

Why do you want to restrict CPU usage to 30%? In Windows I run CPU
intesive therads on IDLE priority, while interfacand/or communication
threads run on normal. This gives me best of two worlds:
1. I use 100% CPU (good) and
2. progam i responcive (very good).

There is no cross platform way to change the thread priority. But most
OS (as well as thread libraries) support setting priorities.

Jul 19 '05 #9
el*******@hotma il.com wrote:
I understand, that what I suggest does not solve the problem you want,
but..

Why do you want to restrict CPU usage to 30%? In Windows I run CPU


there might be three reasons:
1) less power consumed (notebooks, PDA's)
2) less heat from CPU
3) (cross platform) scheduling of low priority tasks (e.g. all my
background tasks are already running with lowest priority since I do not
want them to influence my desktop in any way, but still I want some of them to be
of higher priority)

generally, modern OS'es do not provide any ways to schedule tasks with
such constrains, which makes the question perfectly legitimate

--
-----------------------------------------------------------
| Radovan GarabĂ*k http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls .savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13009
by: Frank Esser | last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max server memory to 32MB and leave the min server memory at 0. When my application starts hitting the database hard the memory usage reported through task manager peaks between 41-42MB. I've stopped and restarted the MSSQLserver service and checked that the running values are what I set them to be. Does anybody have any ideas as to why the sqlservr.exe would be utilizing more...
1
1572
by: Arie | last post by:
The question is: I would like to know how to reduce my dot.net application memory usage, I have already tried to the SetProcessWorkingSetSize method with reduced the memory in 7MB, but that’s not good enough. many thanks in davance
8
5263
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
11
2488
by: Paulo Eduardo | last post by:
Hi, All! We are developing one app for windows 95/98/Me/NT4.0/2000/XP/2003 using Visual C++ 6.0. We need to set the % of CPU Usage to app process. Is there an API to set % of CPU Usage? Can Someone help us? Thanks in advance.
4
7829
by: AN | last post by:
Greetings, We make an ASP.NET web application and we host it for our customers. We have provisioned hardware and hope to be able to service around 200 customers on this hardware. The web servers are in a stateless farm and have 2 GB of RAM. We are using ASP.NET 1.1 when using a dedicated application pool for each virtual directory. Each customer gets their own virtual directory and copy of the ASP.NET dll in their bin folder, which...
2
2469
by: jld | last post by:
Hi, I developed an asp.net based eCommerce Website for a client and it is hosted at discount asp. The site is quite interactive, queries a database a lot and uses ajax.asp.net to spice up interactivity. The service suffers from a lot of restarts since discountasp enforces a 100mb per worker thread limit and when you top it, the service gets restarted. When there is a lot of traffic on the site, this happens
1
1901
by: michaelpyles | last post by:
IIS6 (x86) - Classic ASP application - I am downloading files via calls to BinaryWrite from a C++ COM component. The ASP DLL doesn't seem to do a good job limiting memory usage based on the bandwidth and latency of the network. When streaming very large files, memory usage approaches to 2GB limit and sometimes results in out-of-memory errors. I'm considering writing my own throttle in my C++ component that polls private bytes. The only...
104
5252
by: jayapal | last post by:
Hi all, Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). why...? regards, jayapal.
12
9945
by: bomahony | last post by:
Im runnign various versions of DB2 on various UNIX platforms (Solaris / AIX). DB2 is generally eating up about 30% of memory on these systems (4Gb of RAM, which equates to over a gig). i want to limit DB2s memory usage to a couple of hundred meg in total for the entire application, but cannot seem to find out how to do this. When i check the instance memory usage with db2mtrk, it says its only using a few Megs. I cannot find out how...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.