# HG changeset patch # User Henry S. Thompson # Date 1664465622 -3600 # Node ID cbac7dfe2f24bafd8db5e0d728ca96a3f25e75a1 # Parent a5dafc1364ed8b8124b481df8a1b08b3a9e43a32 interpolate process0, support permanent subproc diff -r a5dafc1364ed -r cbac7dfe2f24 bin/ix.py --- a/bin/ix.py Thu Sep 29 16:31:28 2022 +0100 +++ b/bin/ix.py Thu Sep 29 16:33:42 2022 +0100 @@ -27,13 +27,24 @@ FOO) def process(options,buf,filename,offset,length,whole): - global CMD_PROC, BINOUT, TMPFILENAME, TMPFILE + try: + process0(options,buf,filename,offset,length,whole) + except Exception as e: + if options.debug: + import traceback + traceback.print_exc(file=sys.stderr) + else: + print("Process fail: %s, input line:\n %s"%(e,l), + file=sys.stderr,end='') + exit(3) + +def process0(options,buf,filename,offset,length,whole): + global TMPFILENAME, TMPFILE if options.save: (tf,TMPFILENAME)=tempfile.mkstemp() TMPFILE=open(tf,mode='wb') - if options.cmd: - CMD_PROC=Popen(shlex.split(options.cmd),stdin=PIPE,bufsize=0) - BINOUT=CMD_PROC.stdin + if options.cmd and not options.process: + launch(options.cmd) process1(options,buf,filename,offset,length,whole) if options.save: TMPFILE.close() @@ -44,20 +55,28 @@ BINOUT.write(bytes(TMPFILENAME,'utf-8')) BINOUT.write(b"\n") if options.cmd: - # Wind up subproc - BINOUT.close() - if CMD_PROC.wait()!=0: # could/should be async? - print("subproc of %s:%s:%s failed with %s"%(length,offset,filename, - CMD_PROC.returncode), - file=sys.stderr) + if not options.process: + windup(filename,options,length) if options.save: - # not if async? os.unlink(TMPFILENAME) TMPFILENAME=None elif options.save: print("%s will need to be deleted"%TMPFILENAME,file=sys.stderr) TMPFILENAME=None +def launch(cmd): + global CMD_PROC, BINOUT + CMD_PROC=Popen(shlex.split(cmd),stdin=PIPE,bufsize=0) + BINOUT=CMD_PROC.stdin + +def windup(length,offset,filename): + # Wind up subproc + BINOUT.close() + if CMD_PROC.wait()!=0: # could/should be async? + print("subproc of %s:%s:%s failed with %s"%(length,offset,filename, + CMD_PROC.returncode), + file=sys.stderr) + def _output_tmpfile(buf): TMPFILE.write(buf) @@ -113,6 +132,20 @@ state=0 tr=None # Was this record truncated? bl=None # for HTTP Content-Length for the length of the body? + # Could we make this faster by working purely within the cb memoryview? + # It would be messy, but avoid copying huge amounts + # The outer loop would just be something like + # clbv=memoryview(bytearray(b"Content-Length: ")) + # i=s=0 + # while i