如何访问不同工作人员的共享内存

时间:2017-10-31 20:32:58

标签: python multithreading asynchronous celery

Share memory areas between celery workers on one machine

在这篇文章中,作者演示了如何在工人之间共享字典。多亏了他,我已经实现了这一点。但现在我想从main.py中读取共享内存。

我有main.py.我有generatereport.py生成报告,我在这里添加了共享字典。所有工人现在都可以访问相同的字典。现在我如何从main访问这个共享字典。

main.py

from report import reportgen
from flask import Flask, request, send_from_directory, url_for

app = Flask(__name__)

@ns.route('/GenerateReport')
@ns.doc('Generate PDF')
class GeneratePdf(Resource):
    def get(self):
        task = generate.delay(myDict, uuid)
        return {"request":"made"}

@ns.route('/pdfstatus')
@ns.doc('status')
class statusPdf(Resource):
    def get(self):
        print(report.serviceStatusDict)

cel.py

from celery import Celery
from report import reportgen

app = Celery('tasks', broker='pyamqp://guest@localhost//')

@app.task
def generate(myDict, name):
    data = generateJson(myDict)
    pdfgen.serviceStatusDict[name] = "Rest Performed"
    reportgen(data, name)

report.py

from multiprocessing import Manager
manag = Manager()
serviceLock = manag.Lock()
serviceStatusDict = manag.dict()
def reportgen():
   serviceStatusDict["uuid"] = "Generated the report"
   //....//

0 个答案:

没有答案