문제

I'm working on trying to get a print server running using this: http://newcenturycomputers.net/projects/rawprintserver.html

I've modified spooler.py to write to a file and everything is working OK save for one issue: the job seems to take a long time considering what is being printed...

The job gets received and it chugs along for a solid minute before actually finishing. This seems like an excessively long time for a 1kb txt file with the word 'test' in it...

It exhibits the same behavior whether using Windows or Mac, so it's not an OS thing...

This is the modified def in spooler.py

class printer(base_printer):
    def sendjob(self, fp, title = None):
        # title is irrelevant here
        print "Begin sendjob def..."
        out = open('/tmp/printserv/outs/' + str(time.time()).split('.')[0]+".txt", 'w')
        blk = fp.read(8192)
        while blk:
            out.write(blk)
            print "Block written..."
            blk = fp.read(8192)
            print "while loop done..."
        rc = out.close()
        if rc is not None:
            print "Error: lpr returns %02x" % rc
        else:
            print "OK DONE"

This is the main print handler class (from printserver.py) that handles the job:

class print_handler(asyncore.dispatcher):

    def __init__(self, conn, addr, server, jobnumber):
        asyncore.dispatcher.__init__(self, sock = conn)
        self.addr = addr
        self.server = server
        self.jobname = JOBNAME % jobnumber
        self.fp = open(self.jobname, "wb")
        print "Receiving Job from %s for Printer <%s> (Spool File %s)" \
            % (addr, self.server.printer.printer_name, self.jobname)

    def handle_read(self):
        data = self.recv(8192)
        print "Starting handle_read"
        if self.fp:
            print "begin self.fp.write..."
            self.fp.write(data)
            print "end self.fp.write.."

    def writable(self):
        print "Writable return 0"
        return 0

    def handle_write(self):
        print "handle_write pass"
        pass

    def handle_close(self):
        print "Printer <%s>: Printing Job %s" \
            % (self.server.printer.printer_name, self.jobname)
        if self.fp:
            self.fp.close()
            self.fp = None
        fp = open(self.jobname, "rb")
        self.server.printer.sendjob(fp)
        fp.close()
        try:
            os.remove(self.jobname)
        except:
            print "Can't Remove <%s>" % self.jobname
        self.close()

and the mainloop def (also in printserver.py):

def mainloop(config):

    if config["spooldir"]:
        os.chdir(config["spooldir"])

    for i in range(len(config["printer"])):
        args = string.split(config["printer"][i], ",", maxsplit = 1)
        prn = args[1].strip()
        port = int(args[0].strip())
        p = print_server('',
            port, spooler.printer(prn))
        servers.append(p)

    try:
        try:
            asyncore.loop(timeout = 1.0)
        except KeyboardInterrupt:
            pass
    finally:
        print "Print Server Exit"

those print's get logged in a logfile which looks like this:

[2012/04/26 10:39:58] Raw Print Server Startup: PID = 15695
[2012/04/26 10:39:58] Starting Printer <lpr> on port 515
[2012/04/26 10:40:01] Receiving Job from ('1.2.3.4', 55070) for Printer <lpr> (Spool File RawPrintJob00001.prn)
[2012/04/26 10:40:01] Writable return 0
[2012/04/26 10:40:01] Starting handle_read
[2012/04/26 10:40:01] begin self.fp.write...
[2012/04/26 10:40:01] end self.fp.write..
[2012/04/26 10:40:01] Writable return 0
[2012/04/26 10:40:02] Writable return 0
[2012/04/26 10:40:03] Writable return 0
[2012/04/26 10:40:04] Writable return 0
                 [[SNIP]]
[2012/04/26 10:41:39] Writable return 0
[2012/04/26 10:41:40] Writable return 0
[2012/04/26 10:41:41] Writable return 0
[2012/04/26 10:41:42] Starting handle_read
[2012/04/26 10:41:42] begin self.fp.write...
[2012/04/26 10:41:42] end self.fp.write..
[2012/04/26 10:41:42] Writable return 0
[2012/04/26 10:41:42] Printer <lpr>: Printing Job RawPrintJob00001.prn
[2012/04/26 10:41:42] Begin sendjob def...
[2012/04/26 10:41:42] Block written...
[2012/04/26 10:41:42] done writing blocks...
[2012/04/26 10:41:42] OK DONE
[2012/04/26 10:41:42] Starting handle_read

at this point, the job is finished and the file has been written. Nothing else gets logged until the next job comes in. As you can see, this took over 1.5 minutes to complete.

My question is: how could it possibly take almost 2 minutes to handle a 1k file? This is all local on gigabit LAN so it must be something with the way the code is handling the job... One thing I have noticed is that the printed jobs seem to have a lot of trailing whitespace. Could it be that it is looking for an EOF or something and eventually just timing out?

Any pointers appreciated

도움이 되었습니까?

해결책

This turned out be an issue with how the printer was configured on the client. It was set to lpr instead of to RAW.

Coincidentally, I came across a perl script that will do the same thing as the py script (kinda). With the perl script, there is much less code to achieve the same goal (that goal for me being to simply catch a stream and write to a file; rps is more verbose than I need).

Here:

#!/usr/bin/perl 
use IO::Socket::INET;
use POSIX qw(strftime);
$print_time = strftime "%Y%m%d-%H_%M_%S", localtime; 
$myport=515; 
$pserve=IO::Socket::INET->new(LocalPort => $myport,Type=>SOCK_STREAM,Reuse=>1,Listen=>1) or die "Socket error: $!\n"; 
while ($pjob=$pserve->accept()) {
    open(J,">>/tmp/printserv/outs/".$print_time.".txt") or print "File open error: $!\n"; 
    while (<$pjob>) { 
        print J "$_"; 
    } 
    close J; 
    close $pjob;
    print  "DONE..."; 
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top