def
repatching(item):
try
:
# 需要重新写回的module
module
=
__import__
(item)
# 需要重写的属性
saved
=
gevent.monkey.saved
mapper
=
saved.get(item, {})
for
attr
in
mapper:
old_value
=
mapper.get(attr)
if
not
old_value:
continue
setattr
(module, attr, old_value)
except
Exception, ex:
logger.exception(
"[gevent] repatching fail error=%s"
%
ex)
class
ParallelTask(
object
):
def
__init__(
self
, timeout
=
5
):
# 覆盖python原生的socket, 使用完记得repatching 不然会有未知错误
gevent.monkey.patch_socket()
self
.timeout
=
timeout
self
.task
=
[]
def
taskAppend(
self
, task,
*
args):
self
.task.append(gevent.spawn(task,
*
args))
def
run(
self
):
errno, err_msg
=
get_error(AwemeStatus.SUCCESS)
try
:
gevent.joinall(
self
.task, timeout
=
self
.timeout)
return
errno, err_msg
except
Exception, ex:
logger.exception(
"[ParallelTask] run task fail error=%s"
%
ex)
errno, err_msg
=
get_error(AwemeStatus.REE_PARALLEL_TASK)
return
errno, err_msg
finally
:
# 使用完非阻塞的网络io之后一定要改回来
repatching(
‘socket‘
)