By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Running a python program during idle time only

P: n/a
los
Hi,

I'm trying to create a program similar to that of Google's desktop that
will crawl through the hard drive and index files. I have written the
program and as of now I just put the thread to sleep for 1 second after
indexing a couple of files.

I'm wondering if anyone knows of a way that I could make so that the
program will run at full speed only runs after the computer has been
idle for a while. I've looked at the "nice" command but that's not
exactly what I want.

Let me know if it isn't clear what I explained above.

Thanks

Jul 19 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
"los" <ca******@gmail.com> writes:
I'm trying to create a program similar to that of Google's desktop that
will crawl through the hard drive and index files. I have written the
program and as of now I just put the thread to sleep for 1 second after
indexing a couple of files.

I'm wondering if anyone knows of a way that I could make so that the
program will run at full speed only runs after the computer has been
idle for a while. I've looked at the "nice" command but that's not
exactly what I want.


On Unix, nice is exactly the answer. It's a lot more fine-grained than
what you're talking about, though. But it's the way things like
setiathome manage to run continuously without interfering with normal
usage.

If that's to fine grained for you, you could try waking up every few
minutes and checking the load average (via os.getloadavg), and only
doing work if the load average is low - say less than .5. While
running, you check the load average every couple of minutes and stop
if it rises noticably above 1 (you). The problem with this approach is
that it doesn't deal well with multiple tools doing this. I.e. - if
you run setiathome, your load average will always be above 1, so you
have to adjust your check value to take into account anything else
that might be in the background. Nice handles this automatically.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #2

P: n/a
Quoth Mike Meyer <mw*@mired.org>:
| "los" <ca******@gmail.com> writes:
| > I'm trying to create a program similar to that of Google's desktop that
| > will crawl through the hard drive and index files. I have written the
| > program and as of now I just put the thread to sleep for 1 second after
| > indexing a couple of files.
| >
| > I'm wondering if anyone knows of a way that I could make so that the
| > program will run at full speed only runs after the computer has been
| > idle for a while. I've looked at the "nice" command but that's not
| > exactly what I want.
|
| On Unix, nice is exactly the answer. It's a lot more fine-grained than
| what you're talking about, though. But it's the way things like
| setiathome manage to run continuously without interfering with normal
| usage.

Well, he could certainly try it if he hasn't already, but I wouldn't
be too surprised if it really isn't exactly what he wants. Maybe it
depends on the scheduler, and maybe things have gotten a lot better
in that department, but from my experience a process can be niced pretty
hard and still tie up a lot of resources. A disk crawler sounds like
a good example.

I don't have any brilliant ideas, though. Your suggestion to use
os.getloadavg sounds good to me. It might be kind of neat if there
were some kind of APM ioctl that would register the calling process
for notification of pending disk spin down, low power mode etc.

Donn
Jul 19 '05 #3

P: n/a
There's probably a way to tell how long the user has been idle, but here's
how you can check the CPU load to see if it's elevated... (of course
your program might elevate it too.)

On linux, you can read from /proc/loadavg

Here's a fun thing to try on windows...

make sure you have the win32all package installed...

import win32com.client
computer="."
query="select LoadPercentage from Win32_Processor where DeviceID = 'CPU0'"
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocat or")
objSWbemServices = objWMIService.ConnectServer(computer,"root\cimv2")
rows = objSWbemServices.ExecQuery(query)
print rows[0].LoadPercentage

import time

for count in xrange(4):
print objSWbemServices.ExecQuery(query)[0].LoadPercentage
time.sleep(1)

The microsoft docs on the info you can get on the processor is here:
http://msdn.microsoft.com/library/de..._processor.asp

If you run this from a shell like PyCrust, you won't see anything for
four seconds, and then it will all show up.

What are you using for the text indexing? PyLucene???

-Jim

On 5/21/05, Donn Cave <do**@drizzle.com> wrote:
Quoth Mike Meyer <mw*@mired.org>:
| "los" <ca******@gmail.com> writes:
| > I'm trying to create a program similar to that of Google's desktop that
| > will crawl through the hard drive and index files. I have written the
| > program and as of now I just put the thread to sleep for 1 second after
| > indexing a couple of files.
| >
| > I'm wondering if anyone knows of a way that I could make so that the
| > program will run at full speed only runs after the computer has been
| > idle for a while. I've looked at the "nice" command but that's not
| > exactly what I want.
|
| On Unix, nice is exactly the answer. It's a lot more fine-grained than
| what you're talking about, though. But it's the way things like
| setiathome manage to run continuously without interfering with normal
| usage.

Well, he could certainly try it if he hasn't already, but I wouldn't
be too surprised if it really isn't exactly what he wants. Maybe it
depends on the scheduler, and maybe things have gotten a lot better
in that department, but from my experience a process can be niced pretty
hard and still tie up a lot of resources. A disk crawler sounds like
a good example.

I don't have any brilliant ideas, though. Your suggestion to use
os.getloadavg sounds good to me. It might be kind of neat if there
were some kind of APM ioctl that would register the calling process
for notification of pending disk spin down, low power mode etc.

Donn
--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #4

P: n/a
| ....
| I'm wondering if anyone knows of a way that I could make so that
| the program will run at full speed only runs after the computer
| has been idle for a while.

Perhaps looking at some ScreenSaver code could be useful ....

I've never considered how this is done myself,
but ScreeSavers seem to know that a user hasn't
done anything for a while ....
--
Stanley C. Kitching
Human Being
Phoenix, Arizona

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 19 '05 #5

P: n/a
On Friday 20 May 2005 08:06 pm, los wrote:
I'm wondering if anyone knows of a way that I could make so that the
program will run at full speed only runs after the computer has been
idle for a while. I've looked at the "nice" command but that's not
exactly what I want.


You need to define what "idle" means: Screensavers do this in more
than one way, but typically, "idle" means "no user input has occured",
in which case you are looking for the time since the last input signal
from keyboard or mouse (not sure how to do this off the top of my
head, but that's the "screensaver" approach).

This is much less meaningful on a multiuser / multiple terminal
machine, of course. And it's really nonsense on a machine that is
also acting as a server. That's why "nice" seems like a more logical
solution for these types of machines.

Someone's already mentioned checking the load average, which
sounds like a good idea for your application.
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #6

P: n/a
los
Thanks for all the replies.

I did try using nice under windows. I created a java program that
would just loop and print numbers on the screen. Even when I ran that
simple program with nice, (lets call it program A) as soon as I started
the program the cpu went all the way to 100% usage. Then when I ran
another program that did the same thing (lets call it program B),
program A halted to let B finish, then it started again. Nevertheless
it still hogged all the cpu while I was using the computer.

For my indexing program I just wrote a simple python program and called
on the python os.walk() method to iterate through the drive and then it
connects to a database to store some information. Then I wrote a
simple interface to connect to the database to search for files using
visual basic. Once everything is indexed it works fine, but it would
be nice to have the program looping through and indexing the files all
the time to account to file updates, deletes, and relocation, but
without hurting the performance when I'm using the computer.

So really what I am looking for is some way to have the program only
start indexing and crawling through the hd after 5 minutes of no user
interaction with the computer.

I'm going to take a look at this CPU load possibility. But I'm afraid
that this will work similarly to "nice" in which case it will let the
program kick in when the CPU isn't being used heavily, but I might
still be using the computer.

thanks once again!

-los

Jul 19 '05 #7

P: n/a
I think you can keep your sleep commands in your program to keep it
from hogging the cpu even when you are running it as nice.

You know, even more important than cpu load (since your indexer is
accessing the hard drive, is hard drive access..) You can monitor the
bytes / second going to the hard drives using a WMI query similar to
the one that gives you LoadPercentage for a cpu.

If something Is trying to read and write to the hard drive, and your
indexer is going at the same time, hard drive head contention can slow
down both processess to a crawl. (Say your program is in C:/apps and
another program is simutaneously trying to read from C:/data... the
heads have to seek back and forth between the two spots on the hard
drive, and it's much faster to do all the C:/apps accesses and then
later do all the C:/data accesses.)

So I think my approach would be to have the indexer take about 10% of
cpu load while it is active, and as soon as another process is doing
enough reading / writing to the hard drive, stop and wait for about
five minutes... then continuing.

The screen saver idea is another good one. I found this the other day...
http://homepage.hispeed.ch/py430/pyt...aver-0.3.2.zip

The problem is that any potential user that really likes their pretty
screen saver (Helios under Ubuntu... droool............ slurp.) then
they can't have both your indexer and their pretty screensaver active
during idle time.

-Jim
On 23 May 2005 10:32:18 -0700, los <ca******@gmail.com> wrote:
Thanks for all the replies.

I did try using nice under windows. I created a java program that
would just loop and print numbers on the screen. Even when I ran that
simple program with nice, (lets call it program A) as soon as I started
the program the cpu went all the way to 100% usage. Then when I ran


<snip>
Jul 19 '05 #8

P: n/a
I think you can keep your sleep commands in your program to keep it
from hogging the cpu even when you are running it as nice.

You know, even more important than cpu load (since your indexer is
accessing the hard drive, is hard drive access..) You can monitor the
bytes / second going to the hard drives using a WMI query similar to
the one that gives you LoadPercentage for a cpu.

If something Is trying to read and write to the hard drive, and your
indexer is going at the same time, hard drive head contention can slow
down both processess to a crawl. (Say your program is in C:/apps and
another program is simutaneously trying to read from C:/data... the
heads have to seek back and forth between the two spots on the hard
drive, and it's much faster to do all the C:/apps accesses and then
later do all the C:/data accesses.)

So I think my approach would be to have the indexer take about 10% of
cpu load while it is active, and as soon as another process is doing
enough reading / writing to the hard drive, stop and wait for about
five minutes... then continuing.

The screen saver idea is another good one. I found this the other day...
http://homepage.hispeed.ch/py430/pyt...aver-0.3.2.zip

The problem is that any potential user that really likes their pretty
screen saver (Helios under Ubuntu... droool............ slurp.) then
they can't have both your indexer and their pretty screensaver active
during idle time.

-Jim



On 23 May 2005 10:32:18 -0700, los <ca******@gmail.com> wrote:
Thanks for all the replies.

I did try using nice under windows. I created a java program that
would just loop and print numbers on the screen. Even when I ran that
simple program with nice, (lets call it program A) as soon as I started
the program the cpu went all the way to 100% usage. Then when I ran
another program that did the same thing (lets call it program B),
program A halted to let B finish, then it started again. Nevertheless
it still hogged all the cpu while I was using the computer.

For my indexing program I just wrote a simple python program and called
on the python os.walk() method to iterate through the drive and then it
connects to a database to store some information. Then I wrote a
simple interface to connect to the database to search for files using
visual basic. Once everything is indexed it works fine, but it would
be nice to have the program looping through and indexing the files all
the time to account to file updates, deletes, and relocation, but
without hurting the performance when I'm using the computer.

So really what I am looking for is some way to have the program only
start indexing and crawling through the hd after 5 minutes of no user
interaction with the computer.

I'm going to take a look at this CPU load possibility. But I'm afraid
that this will work similarly to "nice" in which case it will let the
program kick in when the CPU isn't being used heavily, but I might
still be using the computer.

thanks once again!

-los

--
http://mail.python.org/mailman/listinfo/python-list

Jul 19 '05 #9

P: n/a
"los" <ca******@gmail.com> writes:
Thanks for all the replies.

I did try using nice under windows. I created a java program that
would just loop and print numbers on the screen. Even when I ran that
simple program with nice, (lets call it program A) as soon as I started
the program the cpu went all the way to 100% usage. Then when I ran
another program that did the same thing (lets call it program B),
program A halted to let B finish, then it started again. Nevertheless
it still hogged all the cpu while I was using the computer.
Actually, CPU usage going to 100% when you run a cpu-intensive program
under nice is what you expect - even want. The idea is that the nice'd
prog soaks up all the "unused" cpu on the system. You don't say whether
program B was niced or not, but from the description, it wasn't. And you
got the behavior I thought you wanted: the nice'd program quit using
any CPU at all while the normal program was running.

At least, that's the behavior I want from my backgrounded tasks.
For my indexing program I just wrote a simple python program and called
on the python os.walk() method to iterate through the drive and then it
connects to a database to store some information. Then I wrote a
simple interface to connect to the database to search for files using
visual basic. Once everything is indexed it works fine, but it would
be nice to have the program looping through and indexing the files all
the time to account to file updates, deletes, and relocation, but
without hurting the performance when I'm using the computer.
As has been pointed out, your daemon is doing disk i/o, which nice won't
mediate properly.
So really what I am looking for is some way to have the program only
start indexing and crawling through the hd after 5 minutes of no user
interaction with the computer.
Why 5 minutes? What if you've gone to get a cup of coffee while something
that takes more than five minutes is completing?

I'm sure there are tools for doing this kind of thing on windows. That's
pretty much how screen savers work. I haven't written a screen saver
for windows, though - so I have no idea what you're looking for.

Note that the screen savers I have written still lowered their priority
to avoid interfering with any long-running tasks you may be waiting on.
At least for CPU usage.
I'm going to take a look at this CPU load possibility. But I'm afraid
that this will work similarly to "nice" in which case it will let the
program kick in when the CPU isn't being used heavily, but I might
still be using the computer.


I think you're right.

On a completely different topic, this looks like the wrong way to solve
the problem. You want to update a search engine based on changes to the
underlying file system. The right way to do this isn't to just keep
rescanning the file system, it's to arrange things so that your scanner
gets notified of any changes made to the file system. I did something like
this for my web site search engine, but that's hooked into the SCM that's
used for propogating changes to the web site. I know someone is working
on patches to the FreeBSD kernel to make this kind of thing work. It would
seem that some of the "backup" facilities that worked by keeping a mirror
of the disk on separate media would have to have used such hooks, but maybe
not.

I'm not sure this is possible. If it is, it's almost certainly deep magic.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #10

P: n/a
los wrote:
I'm trying to create a program similar to that of Google's desktop that will crawl through the hard drive and index files. I have written the program and as of now I just put the thread to sleep for 1 second after indexing a couple of files.

I'm wondering if anyone knows of a way that I could make so that the
program will run at full speed only runs after the computer has been
idle for a while. I've looked at the "nice" command but that's not
exactly what I want.


On Windows:

1) You can use API calls in the win32file.ReadDirectoryChangesW()
family to register to receive notification (event-based, not
polling-based) of changes to one or more directory structures.

2) You can register to be informed X amount of time after the computer
"becomes idle", as explained at
http://msdn.microsoft.com/library/de...e_triggers.asp
:
"""
An idle trigger is an event-based trigger that is fired a specific
amount of time after the computer becomes idle. The computer is
considered to be in an idle state when no keyboard or mouse input
occurs.
"""
I haven't actually written any code to do this, but it appears to be
accessible from Python via the win32com.taskscheduler.taskscheduler
module; interface
PyIScheduledWorkItem.[CreateTrigger(TASK_EVENT_TRIGGER_ON_IDLE) +
SetIdleWait(...)].

There are related example programs in
site-packages\win32comext\taskscheduler\test\test_addta sk*.py

3) You can programmatically lower the priority of your process with
win32process.SetPriorityClass().

---

So if you combine the three, you can create an indexing program that:

a) Only operates when changes are actually made to the file system (no
busy-wait).

b) Defers all processing until the computer is "idle"; or also runs
when the computer is not idle, yet yields priority to other processes;
or runs under both conditions, but with a more aggressive priority when
the computer is idle.

Jul 19 '05 #11

P: n/a
"los" <ca******@gmail.com> writes:
Thanks for all the replies.

I did try using nice under windows. I created a java program that
would just loop and print numbers on the screen. Even when I ran that
simple program with nice, (lets call it program A) as soon as I started
the program the cpu went all the way to 100% usage. Then when I ran
another program that did the same thing (lets call it program B),
program A halted to let B finish, then it started again. Nevertheless
it still hogged all the cpu while I was using the computer.


Well, shouldn't it? You don't want to waste CPU cycles. If your
program is niced to a low priority, it will run _only when more
important tasks doesn't run_. I.e.: It stays out of the way when
higher priority processes claim the computer, while still utilizing
the rest of the computational power.

My experience is that properly niced processes can use almost 100
percent CPU, and still not have any noticeable effect on my other use
of the computer.

Asbj.S.
--
Asbjørn Sæbø, post.doc.
Centre for Quantifiable Quality of Service in Communication Systems
Norwegian University of Science and Technology
<URL: http://www.q2s.ntnu.no/ >
Jul 19 '05 #12

P: n/a
los
Yes it should. The problem is that I notice a loss in performance when
the program is running at 100% CPU. Even though it is nice, if you try
to open up new applications, or switch between them, you notice your
computer lagging a little bit. That's why even though I'm not using
the cpu that much as I'm just reading my email, or browsing the web, I
wouldn't necessarily want the indexing program to be running at full
pace. If you use google's desktop program, you'll notice that once the
computer has been idle for a few minutes your CPU will go to 100%, but
as soon as you move your mouse, or hit a keystroke the cpu will drop to
5-10% usage. That's the behavior I'm trying to achieve.

Thanks David for your suggestions. That seems to be what I was looking
for. I'm going to try to incorporate those changes in my code and see
what I can come up with. Thanks for everyone who responded as well. I
really appreciate it.

-los

Jul 19 '05 #13

P: n/a
Mike Meyer wrote:
On a completely different topic, this looks like the wrong way to solve
the problem. You want to update a search engine based on changes to the
underlying file system. The right way to do this isn't to just keep
rescanning the file system, it's to arrange things so that your scanner
gets notified of any changes made to the file system. I did something like
this for my web site search engine, but that's hooked into the SCM that's
used for propogating changes to the web site. I know someone is working
on patches to the FreeBSD kernel to make this kind of thing work. It would
seem that some of the "backup" facilities that worked by keeping a mirror
of the disk on separate media would have to have used such hooks, but maybe
not.


I think you're right that filesystem change notification is what Carlos
needs.

If you're interested in using Linux, Carlos, "inotify" is a new kernel
module that can notify your program of filesystem changes. It's not
folded into the mainline kernel yet, but it's a clean patch.

http://www.edoceo.com/creo/inotify/

I don't know if Windows has anything like it. I'd be interested to hear
if it does.

Shane
Jul 19 '05 #14

P: n/a
Shane Hathaway wrote:
Mike Meyer wrote:

On a completely different topic, this looks like the wrong way to solve
the problem. You want to update a search engine based on changes to the
underlying file system. The right way to do this isn't to just keep
rescanning the file system, it's to arrange things so that your scanner
gets notified of any changes made to the file system. I did something like
this for my web site search engine, but that's hooked into the SCM that's
used for propogating changes to the web site. I know someone is working
on patches to the FreeBSD kernel to make this kind of thing work. It would
seem that some of the "backup" facilities that worked by keeping a mirror
of the disk on separate media would have to have used such hooks, but maybe
not.


I think you're right that filesystem change notification is what Carlos
needs.

If you're interested in using Linux, Carlos, "inotify" is a new kernel
module that can notify your program of filesystem changes. It's not
folded into the mainline kernel yet, but it's a clean patch.

http://www.edoceo.com/creo/inotify/

I don't know if Windows has anything like it. I'd be interested to hear
if it does.

Shane

Using the PyWin32 extensions, you can register an event with the kernel,
and then have the script sleep. If I can remember how, I'll post some
code. It's been a while since I coded specific Win32 stuff.

J
Jul 19 '05 #15

P: n/a
John Abel wrote:
Shane Hathaway wrote:
Mike Meyer wrote:

On a completely different topic, this looks like the wrong way to solve
the problem. You want to update a search engine based on changes to the
underlying file system. The right way to do this isn't to just keep
rescanning the file system, it's to arrange things so that your scanner
gets notified of any changes made to the file system. I did something like
this for my web site search engine, but that's hooked into the SCM that's
used for propogating changes to the web site. I know someone is working
on patches to the FreeBSD kernel to make this kind of thing work. It would
seem that some of the "backup" facilities that worked by keeping a mirror
of the disk on separate media would have to have used such hooks, but maybe
not.

I think you're right that filesystem change notification is what Carlos
needs.

If you're interested in using Linux, Carlos, "inotify" is a new kernel
module that can notify your program of filesystem changes. It's not
folded into the mainline kernel yet, but it's a clean patch.

http://www.edoceo.com/creo/inotify/

I don't know if Windows has anything like it. I'd be interested to hear
if it does.

Shane

Using the PyWin32 extensions, you can register an event with the kernel,
and then have the script sleep. If I can remember how, I'll post some
code. It's been a while since I coded specific Win32 stuff.

J

Couldn't find my code, but this page has various ways of doing it on Win32.

http://tgolden.sc.sabren.com/python/...r_changes.html

HTH

J
Jul 19 '05 #16

P: n/a
Shane Hathaway wrote:
Mike Meyer wrote:

On a completely different topic, this looks like the wrong way to solve
the problem. You want to update a search engine based on changes to the
underlying file system. The right way to do this isn't to just keep
rescanning the file system, it's to arrange things so that your scanner
gets notified of any changes made to the file system. I did something like
this for my web site search engine, but that's hooked into the SCM that's
used for propogating changes to the web site. I know someone is working
on patches to the FreeBSD kernel to make this kind of thing work. It would
seem that some of the "backup" facilities that worked by keeping a mirror
of the disk on separate media would have to have used such hooks, but maybe
not.


I think you're right that filesystem change notification is what Carlos
needs.

If you're interested in using Linux, Carlos, "inotify" is a new kernel
module that can notify your program of filesystem changes. It's not
folded into the mainline kernel yet, but it's a clean patch.

http://www.edoceo.com/creo/inotify/

I don't know if Windows has anything like it. I'd be interested to hear
if it does.

Shane

As an alternative to inotify there's this
http://oss.sgi.com/projects/fam/, with various libraries ( Perl, Python,
etc ).

J
Jul 19 '05 #17

P: n/a
los
I'm writting the code with the win32 module I found. The way I'm
planning on attacking the problem after reading all the remarks is as
follows:

1.) have the program run a full system index, and once it completes it
turn the flag on a configuration file to true
2.) after this initial index, every time the program is started it will
check this flag in the configuration file, and if it has had the full
system index then the system will be kept up to date using the win32
modules, listening for events and changes made to the file system. I
am basing my code pretty closely to what I found in the last example of
this page
http://tgolden.sc.sabren.com/python/...r_changes.html

I'm aware that in case the program isn't running, all the changes made
to the file system during that time won't be picked up next time the
program starts. I haven't thought of a good way to solve this problem
other than just do a full system index every once in a while to
actually go through each directory updating and deleting records as
necessary.

Thanks once again for the posts!

-los

Jul 19 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.