Re: Unix Device File Emulation

2008-04-25 Thread blaine
On Apr 24, 3:38 am, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote:
> On 2008-04-23, blaine <[EMAIL PROTECTED]> wrote:
>
> > On Apr 23, 2:01 pm, "Martin Blume" <[EMAIL PROTECTED]> wrote:
> >> "blaine" schrieb
> >> No,
> >>   while 1:
> >>   r = self.fifodev.readline()
> >>   if r: print r
> >>   else: time.sleep(0.1)
> >> is ok (note the "if r:" clause).
>
> >> Martin
>
> > Beautiful! Thanks Martin!
>
> yes, but you have to follow the usual file handling rules, and close/re-open
> the fifo after detecting EOF. You will be blocked on attempting to re-open
> until there is another writing process.
>
> while 1:
>   fp = open('my_fifo', 'r')
>   while 1:
>  line = fp.readline()
>  if line == '':
> break
>  print line.rstrip()# To prevent printing of \n in line
>   fp.close()
>
> Albert

Oh, good call. Thanks a lot, that really helps.  I felt that the
previous solution was workable, but not perhaps 100% correct.

Thanks Albert!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unix Device File Emulation

2008-04-24 Thread A.T.Hofkamp
On 2008-04-23, blaine <[EMAIL PROTECTED]> wrote:
> On Apr 23, 2:01 pm, "Martin Blume" <[EMAIL PROTECTED]> wrote:
>> "blaine" schrieb

>> No,
>>   while 1:
>>   r = self.fifodev.readline()
>>   if r: print r
>>   else: time.sleep(0.1)
>> is ok (note the "if r:" clause).
>>
>> Martin
>
> Beautiful! Thanks Martin!

yes, but you have to follow the usual file handling rules, and close/re-open
the fifo after detecting EOF. You will be blocked on attempting to re-open
until there is another writing process.

while 1:
  fp = open('my_fifo', 'r')
  while 1:
 line = fp.readline()
 if line == '':
break
 print line.rstrip()# To prevent printing of \n in line
  fp.close()

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


Re: Unix Device File Emulation

2008-04-23 Thread blaine
On Apr 23, 2:01 pm, "Martin Blume" <[EMAIL PROTECTED]> wrote:
> "blaine" schrieb
>
>
>
> > > while 1:
> > >  r = self.fifodev.readline()
> > >  if r: print r
>
> > > According to my docs, readline() returns an empty
> > > string at the end of the file.
> > > Also, you might want to sleep() between reads a
> > > little bit.
>
> > Oh ok, that makes sense.  Hmm.  So do I not want to use
> > readline()? Or is there a way to do something like
> > 'block until the file is not empty'?
>
> No,
>   while 1:
>   r = self.fifodev.readline()
>   if r: print r
>   else: time.sleep(0.1)
> is ok (note the "if r:" clause).
>
> Martin

Beautiful! Thanks Martin!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unix Device File Emulation

2008-04-23 Thread Martin Blume
"blaine" schrieb
> >
> > while 1:
> >  r = self.fifodev.readline()
> >  if r: print r
> >
> > According to my docs, readline() returns an empty 
> > string at the end of the file.
> > Also, you might want to sleep() between reads a 
> > little bit.
> >
> 
> Oh ok, that makes sense.  Hmm.  So do I not want to use 
> readline()? Or is there a way to do something like 
> 'block until the file is not empty'?
>

No, 
  while 1:
  r = self.fifodev.readline()
  if r: print r
  else: time.sleep(0.1)
is ok (note the "if r:" clause).

Martin



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


Re: Unix Device File Emulation

2008-04-23 Thread blaine
On Apr 23, 12:27 pm, "Martin Blume" <[EMAIL PROTECTED]> wrote:
> "blaine" schrieb
>
>
>
>
>
> > # Fake Nokia Screen Emulator
> > import sys, os
>
> > class nokia_fkscrn:
> >   def __init__(self, file):
> > if not os.path.exists(file):
> >   os.mkfifo(file)
> > self.fifodev = open(file, 'r')
> >   def read(self):
> > while 1:
> >   r = self.fifodev.readline()
> >   print r
>
> > nokia = nokia_fkscrn('dev.file')
> > nokia.read()
>
> > This works at first, but when I write to the 'dev.file'
> > for the first time, the text is displayed as intended,
> > but then the program just keeps spitting out blank lines.
> > I can continue to write to the file
> > (using echo 'test\n' > dev.file)
> > and this shows up in my output, but amist a giant mass
> > of scrolling blank lines.  This also causes my CPU
> > usage to shoot up to 100%.
>
> > Any ideas? This is OS X 10.4
>
> while 1:
>  r = self.fifodev.readline()
>  if r: print r
>
> According to my docs, readline() returns an empty string
> at the end of the file.
> Also, you might want to sleep() between reads a little bit.
>
> IMHO. HTH.
> Martin

Oh ok, that makes sense.  Hmm.  So do I not want to use readline()? Or
is there a way to do something like 'block until the file is not
empty'?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unix Device File Emulation

2008-04-23 Thread Martin Blume
"blaine" schrieb
> 
> # Fake Nokia Screen Emulator
> import sys, os
> 
> class nokia_fkscrn:
>   def __init__(self, file):
> if not os.path.exists(file):
>   os.mkfifo(file)
> self.fifodev = open(file, 'r')
>   def read(self):
> while 1:
>   r = self.fifodev.readline()
>   print r
> 
> nokia = nokia_fkscrn('dev.file')
> nokia.read()
> 
> This works at first, but when I write to the 'dev.file' 
> for the first time, the text is displayed as intended, 
> but then the program just keeps spitting out blank lines.  
> I can continue to write to the file 
> (using echo 'test\n' > dev.file) 
> and this shows up in my output, but amist a giant mass 
> of scrolling blank lines.  This also causes my CPU
> usage to shoot up to 100%.
> 
> Any ideas? This is OS X 10.4
>

while 1:
 r = self.fifodev.readline()
 if r: print r

According to my docs, readline() returns an empty string
at the end of the file. 
Also, you might want to sleep() between reads a little bit.


IMHO. HTH.
Martin



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


Re: Unix Device File Emulation

2008-04-23 Thread blaine
On Apr 23, 11:17 am, "Ville M. Vainio" <[EMAIL PROTECTED]> wrote:
> blaine wrote:
> > example usage: echo 'line 0 0 10 10' > /dev/screen
>
> > On the actual embedded device this is handled by a kernel module.  We
> > can spit commands into it as fast as we can and the kernel module can
> > keep up.  This is typical unix device file behavior.
>
> > Any suggestions or advice would be splendid.  Thanks!
>
> Assuming you are on unix, have you considered FIFO's, os.mkfifo()?

Thank you - this is exactly what I need, I believe.  I'm having a
problem though.  The os.mkfifo() works fine, but when I read from the
file my blocking calls dont work as intended...  See below:

# Fake Nokia Screen Emulator
import sys, os

class nokia_fkscrn:
  def __init__(self, file):
if not os.path.exists(file):
  os.mkfifo(file)
self.fifodev = open(file, 'r')
  def read(self):
while 1:
  r = self.fifodev.readline()
  print r

nokia = nokia_fkscrn('dev.file')
nokia.read()

This works at first, but when I write to the 'dev.file' for the first
time, the text is displayed as intended, but then the program just
keeps spitting out blank lines.  I can continue to write to the file
(using echo 'test\n' > dev.file) and this shows up in my output, but
amist a giant mass of scrolling blank lines.  This also causes my CPU
usage to shoot up to 100%.

Any ideas? This is OS X 10.4
-Blaine
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unix Device File Emulation

2008-04-23 Thread Dan Upton
(let's try this again, and actually send it to the list this time)

On Wed, Apr 23, 2008 at 11:02 AM, blaine <[EMAIL PROTECTED]> wrote:
> Hey everyone,
>   So I've got a quick query for advice.
>
>   We have an embedded device in which we are displaying to an LCD
>  device that sits at /dev/screen.  This device is not readily available
>  all the time, so I am needing to write an emulator.  This will
>  basically just monitor a file, /dev/screen for example, and write the
>  commands to a TK or WxWindows canvas.
>
>  So sending 'line 0 0 10 10' will draw a line on my canvas from (0,0)
>  to (10,10).
>
>  My question: Whats the best way to set up a monitor (in python) of
>  this file? Would I simply open up the file for read, check for
>  changes, get any updated data, and clear the file? Or is there some
>  standard way of doing something like this that guarantees no overlap
>  or data loss?
>
>  example usage: echo 'line 0 0 10 10' > /dev/screen
>
>  On the actual embedded device this is handled by a kernel module.  We
>  can spit commands into it as fast as we can and the kernel module can
>  keep up.  This is typical unix device file behavior.
>
>  Any suggestions or advice would be splendid.  Thanks!
>  Blaine
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>

've only interacted with device files from python that I was only
reading from. And I guess technically they were under /proc and /sys,
rather than /dev, although they may be handled the same way.  Anyway,
in that case my method was basically to open the file, read it, close
it, do whatever processing I needed to do on the data, sleep for some
interval...lather, rinse, repeat.  Sleeping for some interval may or
may not be appropriate in your case.  I know in the case of /proc
files and /sys files, the files are generated on demand by the kernel
and relevant kernel structures are basically printed to a string and
copied into a page in memory that the user program can access.  AFAIK,
you can seek back and forth through them, but I don't know whether the
data in the page is updated on a seek, so you may have to close and
reopen the file ever iteration to see what's changed.

HTH,
-dan
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unix Device File Emulation

2008-04-23 Thread Ville M. Vainio

blaine wrote:


example usage: echo 'line 0 0 10 10' > /dev/screen

On the actual embedded device this is handled by a kernel module.  We
can spit commands into it as fast as we can and the kernel module can
keep up.  This is typical unix device file behavior.

Any suggestions or advice would be splendid.  Thanks!


Assuming you are on unix, have you considered FIFO's, os.mkfifo()?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unix Device File Emulation

2008-04-23 Thread Helmut Jarausch

blaine wrote:

Hey everyone,
  So I've got a quick query for advice.

  We have an embedded device in which we are displaying to an LCD
device that sits at /dev/screen.  This device is not readily available
all the time, so I am needing to write an emulator.  This will
basically just monitor a file, /dev/screen for example, and write the
commands to a TK or WxWindows canvas.

So sending 'line 0 0 10 10' will draw a line on my canvas from (0,0)
to (10,10).

My question: Whats the best way to set up a monitor (in python) of
this file? Would I simply open up the file for read, check for
changes, get any updated data, and clear the file? Or is there some
standard way of doing something like this that guarantees no overlap
or data loss?

example usage: echo 'line 0 0 10 10' > /dev/screen

On the actual embedded device this is handled by a kernel module.  We
can spit commands into it as fast as we can and the kernel module can
keep up.  This is typical unix device file behavior.

Any suggestions or advice would be splendid.  Thanks!
Blaine


It looks as if you need something like the Unix 'tail -f' command.
Perhaps here is some help

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/414771
http://mail.python.org/pipermail/python-list/2002-August/157510.html
http://pyinotify.sourceforge.net/


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Unix Device File Emulation

2008-04-23 Thread blaine
Hey everyone,
  So I've got a quick query for advice.

  We have an embedded device in which we are displaying to an LCD
device that sits at /dev/screen.  This device is not readily available
all the time, so I am needing to write an emulator.  This will
basically just monitor a file, /dev/screen for example, and write the
commands to a TK or WxWindows canvas.

So sending 'line 0 0 10 10' will draw a line on my canvas from (0,0)
to (10,10).

My question: Whats the best way to set up a monitor (in python) of
this file? Would I simply open up the file for read, check for
changes, get any updated data, and clear the file? Or is there some
standard way of doing something like this that guarantees no overlap
or data loss?

example usage: echo 'line 0 0 10 10' > /dev/screen

On the actual embedded device this is handled by a kernel module.  We
can spit commands into it as fast as we can and the kernel module can
keep up.  This is typical unix device file behavior.

Any suggestions or advice would be splendid.  Thanks!
Blaine
--
http://mail.python.org/mailman/listinfo/python-list