[Python 3 compatibility] Improved the background process executor to

work with Python 3.
pull/3/head
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
# We need to make string for Python2/3 compatible
if sys.version_info < (3,):
from StringIO import StringIO
from cStringIO import StringIO
def u(x):
return codecs.unicode_escape_decode(x)[0]
return x
else:
from io import StringIO
def u(x):
return x
return x.decode()
def usage():
@ -150,7 +150,7 @@ class ProcessLogger(Thread):
if msg:
self.logger.write(
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
@ -268,8 +268,11 @@ def execute(configs):
'db_file': configs['db_file']
}
try:
reload(sys)
sys.setdefaultencoding('utf8')
except:
pass
# Create seprate thread for stdout and stderr
process_stdout = ProcessLogger('out', configs)

View File

@ -21,7 +21,6 @@ from pickle import dumps, loads
import pytz
from subprocess import Popen, PIPE
import sys
import types
from flask.ext.babel import gettext
from flask.ext.security import current_user
@ -203,7 +202,7 @@ class BatchProcess(object):
signal.signal(signal.SIGINT, signal.SIG_IGN)
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
)
@ -214,6 +213,7 @@ class BatchProcess(object):
def status(self, out=0, err=0):
import codecs
ctime = get_current_time(format='%Y%m%d%H%M%S%f')
stdout = []
@ -226,9 +226,9 @@ class BatchProcess(object):
lines = 0
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)
for line in stream:
logtime = StringIO()
@ -249,8 +249,8 @@ class BatchProcess(object):
break
lines += 1
pos = stream.tell()
log.append([logtime, line[idx:]])
pos = stream.tell()
return pos, completed
@ -321,7 +321,7 @@ class BatchProcess(object):
desc = loads(p.desc)
details = desc
if not isinstance(desc, types.StringTypes):
if isinstance(desc, IProcessDesc):
details = desc.details
desc = desc.message