- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Mac OS X 和 Ubuntu 14.04 上将此代码作为 CherryPy Web 服务运行。通过在 python3 上使用 multiprocessing
,我想以异步方式在 Process Pool
中启动静态方法 worker()
。
相同的代码在 Mac OS X 上完美运行,在 Ubuntu 14.04 中 worker()
无法运行。 IE。通过调试 POST
方法中的代码,我可以看到每一行都已执行 - 来自
reqid = str(uuid.uuid4())
到
return handle_error(202, "Request ID: " + reqid)
在 Ubuntu 14.04 中启动相同的代码,它不运行 worker()
方法,甚至不运行方法顶部的 print()
(这将被记录)。
相关代码如下(我只省略了handle_error()
方法):
import cherrypy
import json
from lib import get_parameters, handle_error
from multiprocessing import Pool
import os
from pymatbridge import Matlab
import requests
import shutil
import uuid
from xml.etree import ElementTree
class Schedule(object):
exposed = True
def __init__(self, mlab_path, pool):
self.mlab_path = mlab_path
self.pool = pool
def POST(self, *paths, **params):
if validate(cherrypy.request.headers):
try:
reqid = str(uuid.uuid4())
path = os.path.join("results", reqid)
os.makedirs(path)
wargs = [(self.mlab_path, reqid)]
self.pool.apply_async(Schedule.worker, wargs)
return handle_error(202, "Request ID: " + reqid)
except:
return handle_error(500, "Internal Server Error")
else:
return handle_error(401, "Unauthorized")
#### this is not executed ####
@staticmethod
def worker(args):
mlab_path, reqid = args
mlab = Matlab(executable=mlab_path)
mlab.start()
mlab.run_code("cd mlab")
mlab.run_code("sched")
a = mlab.get_variable("a")
mlab.stop()
return reqid
####
# to start the Web Service
if __name__ == "__main__":
# start Web Service with some configuration
global_conf = {
"global": {
"server.environment": "production",
"engine.autoreload.on": True,
"engine.autoreload.frequency": 5,
"server.socket_host": "0.0.0.0",
"log.screen": False,
"log.access_file": "site.log",
"log.error_file": "site.log",
"server.socket_port": 8084
}
}
cherrypy.config.update(global_conf)
conf = {
"/": {
"request.dispatch": cherrypy.dispatch.MethodDispatcher(),
"tools.encode.debug": True,
"request.show_tracebacks": False
}
}
pool = Pool(3)
cherrypy.tree.mount(Schedule('matlab', pool), "/sched", conf)
# activate signal handler
if hasattr(cherrypy.engine, "signal_handler"):
cherrypy.engine.signal_handler.subscribe()
# start serving pages
cherrypy.engine.start()
cherrypy.engine.block()
最佳答案
您的逻辑对您隐瞒了问题。 apply_async
方法返回 AsyncResult对象,它充当您刚刚安排的异步任务的处理程序。当您忽略计划任务的结果时,整个事情看起来像是“无声地失败”。
如果您尝试从该任务中获取结果,您就会发现真正的问题。
handler = self.pool.apply_async(Schedule.worker, wargs)
handler.get()
... traceback here ...
cPickle.PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
简而言之,您必须确保传递给 Pool 的参数是 Picklable .
如果实例和类方法所属的对象/类也是可腌制的,则它们是可腌制的。静态方法不可 picklable,因为它们失去了与对象本身的关联,因此 pickle 库无法正确序列化它们。
一般而言,最好避免将 multiprocessing.Pool
调度到与顶级定义函数不同的任何内容。
关于python - 多处理 apply_async() 在 Ubuntu 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822292/
我是 python 的新手,我有一个函数可以为我的数据计算特征,然后返回一个应该处理并写入文件的列表。,..我正在使用 Pool 进行计算,然后使用写入文件的回调函数,但是回调函数没有被调用,我已经在
我试图了解多进程池是如何工作的。在下面的编程中,我创建了一个包含 4 个进程的池。 我使用回调函数调用 apply_async ,该函数应该更新名为 result_list 的列表 import Qu
我希望如果我调用 apply_async在实例方法中并获得其结果,所做的任何更改都将保留为 fork 进程的一部分。但是,似乎每次对 apply_async 的新调用都会创建所述实例的新副本。 采取以
我只是在使用 Python 的多处理模块,但是以下代码正在连续运行,但没有给出任何输出。我究竟做错了什么?我也尝试过 pool.close() 和 pool.join() 但没有效果。 这是我迄今为止
我尝试通过 apply_async 将共享计数器传递给多处理中的任务,但它失败并出现以下错误“RuntimeError:同步对象只能通过继承在进程之间共享”。这是怎么回事 def processLin
我有一个带有 apply_async 的进程池,其中不同的进程需要不同的时间来提供输出。一旦一个进程完成,我就会对其输出进行一些计算。在我想启动另一个进程之后。通过这种方式,我想创建一个无限循环,它启
这是我的代码: import multiprocessing import time import os def worker(): print str(os.getpid()) + " is
使用覆盖率来查看必须测试的内容,并且覆盖率显示旁边必须要测试的内容:send_alert.apply_async() 我知道是celery任务,但是有什么办法可以测试这行代码吗? 理解测试逻辑的其余代
传递给多处理的 apply_async() 的函数内的 print() 不会打印任何内容。 我想最终使用 apply_async 来处理大块的文本文件。因此,我希望脚本在屏幕上打印出已经处理了多少行。
我正在计算大量函数(大约1000000),并且由于它非常耗时,所以我使用multiprocessing.Pool.apply_async函数。但是,当我尝试使用 AsyncResult 类的 .get
import time import multiprocessing def multi_thread(files): q = multiprocessing.Queue() for
我一直在关注文档以尝试了解多处理池。我想到了这个: import time from multiprocessing import Pool def f(a): print 'f(' + st
我在我的应用程序中使用 celery 来运行周期性任务。让我们看下面的简单示例 from myqueue import Queue @perodic_task(run_every=timedelta(
我正在尝试了解使用多处理池的 apply_sync 方法时幕后发生的事情。 谁运行回调方法?是调用apply_async的主进程吗? 假设我发送了一大堆带有回调的 apply_async 命令,然后继
我有一个脚本,其中包括从列表中打开一个文件,然后对该文件中的文本执行某些操作。我正在使用 python 多处理和 Pool 来尝试并行化此操作。脚本的抽象如下: import os from mult
如果我理解正确,apply_async 会立即返回一个 AsyncResult 对象。如果我按以下方式收集这些对象,并仅在所有工作人员完成后才使用 get(),假设值将按照调用函数的顺序是否安全? o
我正在尝试使用 multiprocessing模块,更具体地说是 Pool.apply_async()功能。 这段代码运行良好: import multiprocessing def do():
我们使用的是 django 1.10、Celery 4.1.0 我正在尝试使用 apply_async。这是任务: from celery import Celery app = Celery('my
我在 Python 3.7.3 中遇到一个问题,在处理大型计算任务时,我的多处理操作(使用队列、池和 apply_async)会死锁。 对于小型计算,这个多处理任务工作得很好。然而,当处理较大的进程时
我想调用 pool.apply_async(func) 并在结果可用时立即累积结果,而无需相互等待。 import multiprocessing import numpy as np chrName
我是一名优秀的程序员,十分优秀!