[Python 3 compatibility] Improved the background process executor to

work with Python 3.
This commit is contained in:
Ashesh Vashi 2016-05-15 00:03:21 +05:30
parent 491bd3605b
commit fe0911f285
2 changed files with 15 additions and 12 deletions

View File

@ -45,15 +45,15 @@ import codecs
# SQLite3 needs all string as UTF-8 # SQLite3 needs all string as UTF-8
# We need to make string for Python2/3 compatible # We need to make string for Python2/3 compatible
if sys.version_info < (3,): if sys.version_info < (3,):
from StringIO import StringIO from cStringIO import StringIO
def u(x): def u(x):
return codecs.unicode_escape_decode(x)[0] return x
else: else:
from io import StringIO from io import StringIO
def u(x): def u(x):
return x return x.decode()
def usage(): def usage():
@ -150,7 +150,7 @@ class ProcessLogger(Thread):
if msg: if msg:
self.logger.write( self.logger.write(
str('{0},{1}').format( str('{0},{1}').format(
get_current_time(format='%Y%m%d%H%M%S%f'), msg get_current_time(format='%Y%m%d%H%M%S%f'), u(msg)
) )
) )
return True return True
@ -268,8 +268,11 @@ def execute(configs):
'db_file': configs['db_file'] 'db_file': configs['db_file']
} }
try:
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
except:
pass
# Create seprate thread for stdout and stderr # Create seprate thread for stdout and stderr
process_stdout = ProcessLogger('out', configs) process_stdout = ProcessLogger('out', configs)

View File

@ -21,7 +21,6 @@ from pickle import dumps, loads
import pytz import pytz
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import sys import sys
import types
from flask.ext.babel import gettext from flask.ext.babel import gettext
from flask.ext.security import current_user from flask.ext.security import current_user
@ -203,7 +202,7 @@ class BatchProcess(object):
signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGINT, signal.SIG_IGN)
p = Popen( p = Popen(
cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, close_fds=True, cmd, stdout=PIPE, stderr=None, stdin=None, close_fds=True,
shell=False, preexec_fn=preexec_function shell=False, preexec_fn=preexec_function
) )
@ -214,6 +213,7 @@ class BatchProcess(object):
def status(self, out=0, err=0): def status(self, out=0, err=0):
import codecs
ctime = get_current_time(format='%Y%m%d%H%M%S%f') ctime = get_current_time(format='%Y%m%d%H%M%S%f')
stdout = [] stdout = []
@ -226,9 +226,9 @@ class BatchProcess(object):
lines = 0 lines = 0
if not os.path.isfile(logfile): if not os.path.isfile(logfile):
return 0 return 0, False
with open(logfile, 'r') as stream: with codecs.open(logfile, 'r', 'utf-8') as stream:
stream.seek(pos) stream.seek(pos)
for line in stream: for line in stream:
logtime = StringIO() logtime = StringIO()
@ -249,8 +249,8 @@ class BatchProcess(object):
break break
lines += 1 lines += 1
pos = stream.tell()
log.append([logtime, line[idx:]]) log.append([logtime, line[idx:]])
pos = stream.tell()
return pos, completed return pos, completed
@ -321,7 +321,7 @@ class BatchProcess(object):
desc = loads(p.desc) desc = loads(p.desc)
details = desc details = desc
if not isinstance(desc, types.StringTypes): if isinstance(desc, IProcessDesc):
details = desc.details details = desc.details
desc = desc.message desc = desc.message