Timeout on file write?

Discussion in 'Python' started by Chris Farley, Jun 26, 2004.

  1. Chris Farley

    Chris Farley Guest

    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...
     
    Chris Farley, Jun 26, 2004
    #1
    1. Advertising

  2. Chris Farley

    Larry Bates Guest

    Not tested, but you might want to take a look at:

    http://www.geocities.com/dinceraydin/python/indexeng.html

    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()

    Just possible solutions, not tested.

    HTH,
    Larry Bates
    Syscon, Inc.

    "Chris Farley" <> wrote in message
    news:40dda426$0$32608$...
    > 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...
     
    Larry Bates, Jun 26, 2004
    #2
    1. Advertising

  3. Chris Farley

    Chris Farley Guest

    Larry Bates <> wrote:

    > 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...
     
    Chris Farley, Jun 26, 2004
    #3
  4. Chris Farley wrote:

    > 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
     
    Mathias Waack, Jun 27, 2004
    #4
  5. Chris Farley

    Chris Farley Guest

    Mathias Waack <> wrote:

    > 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!
     
    Chris Farley, Jun 30, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Johnson
    Replies:
    0
    Views:
    3,826
    Bob Johnson
    Aug 7, 2003
  2. Do
    Replies:
    2
    Views:
    6,455
  3. Guoqi Zheng
    Replies:
    4
    Views:
    13,126
    Guoqi Zheng
    Jun 3, 2004
  4. Just D

    IIS timeout, IE timeout or what?

    Just D, Jun 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,565
    Just D
    Jun 21, 2004
  5. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,364
    Brian Candler
    Oct 6, 2004
Loading...

Share This Page