Re: [Possible SPAM] Re: Using msvcrt (in Windows), how to catch Enter key?

2007-10-29 Thread Gabriel Genellina
En Mon, 29 Oct 2007 21:22:36 -0300, Dick Moores <[EMAIL PROTECTED]> escribió:

> At 03:23 PM 10/29/2007, Gabriel Genellina wrote:
>> En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <[EMAIL PROTECTED]>  
>> escribió:
>>
>>
>> At least add a small sleep() call inside the loop, to be nice to other
>> running processes:
>>
>
> Yes, that makes a major difference in the CPU
> usage percentage on my computer. In fact I can't
> even tell that there is anything going on other
> than the usual behind-the-scenes XP stuff. CPU
> usage stays right around 0% or 6%, with an
> occasional 6% and a very occasional 15%.
> Interestingly, sleep(0.001) makes as big a
> difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady  
> 100%!

The underlying function in Windows is Sleep (or SleepEx) which takes an  
argument in milliseconds. 0.0001s = 0.1ms and it's rounded to 0. Sleep(0)  
has very specific semantics - for a single threaded program, it does  
nothing, so your code is effectively a busy loop taking 100% CPU.

-- 
Gabriel Genellina

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


Re: [Possible SPAM] Re: Using msvcrt (in Windows), how to catch Enter key?

2007-10-29 Thread Dick Moores
At 03:23 PM 10/29/2007, Gabriel Genellina wrote:
>En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <[EMAIL PROTECTED]> escribió:
>
> > But here's a case where it seems I do need the
> >
> > if msvcrt.kbhit() line
>
>At least add a small sleep() call inside the loop, to be nice to other
>running processes:
>
> > =
> > #!/usr/bin/env python
> > #coding=utf-8
> > import time
> > import msvcrt
> > timeNow = time.time()
> > oldTimeNow = timeNow
> > while True:
> >  if msvcrt.kbhit():
> >  key = msvcrt.getch()
> >  if key == 'h':
> >  print 'Hello'
> >  if key == 'b':
> >  print 'Bye'
> >  if key == '\r': # Enter key
> >  break
> else:
> time.sleep(0.1)
> >  timeNow = time.time()
> >  if timeNow - oldTimeNow > 5:
> >  print "5 seconds passed"
> >  oldTimeNow = timeNow
> > =

Yes, that makes a major difference in the CPU 
usage percentage on my computer. In fact I can't 
even tell that there is anything going on other 
than the usual behind-the-scenes XP stuff. CPU 
usage stays right around 0% or 6%, with an 
occasional 6% and a very occasional 15%. 
Interestingly, sleep(0.001) makes as big a 
difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady 100%!

Thanks,

Dick 

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