Timeout on file write?

C

Chris Farley

I'm working on a cross-platform Python program that prints to a receipt
printer. The code is simple, I just do something like this:

printer = file('/dev/lp0','w') # on Win32, change to 'lpt1'
p.write("whatever")
p.close()


I would like to gracefully handle situations such as when the paper is out
or the printer is powered off. Right now the program just hangs.

Suggestions? Thanks...
 
C

Chris Farley

Larry Bates said:
second option, you could try something like:
p.write("whatever")
try:
p.write("whatever")
p.flush()
except:
print "Write error on receipt printer"
p.close()

This doesn't work, as the call to flush just hangs if the printer is
powered down. It does not throw an exception.

Would it work to start a new thread, and if the thread doesn't return
in a specified time, destroy it? I've read that Python thread's can not
be interrupted, so I'm a bit skeptical that this would work...
 
M

Mathias Waack

Chris said:
I would like to gracefully handle situations such as when the paper
is out or the printer is powered off. Right now the program just
hangs.

The simplest way would be to use a timeout as described in the python
lib docs, have a look at the signal module.

Mathias
 
C

Chris Farley

Mathias Waack said:
The simplest way would be to use a timeout as described in the python
lib docs, have a look at the signal module.

Thank you, that's exactly what I needed!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top