共享变量不使用Python中的多处理值

发布于 2025-02-06 10:47:21 字数 1595 浏览 2 评论 0原文

我是Python多处理的新手。我正在WSGI Gunicorn多处理环境中部署我的Python代码。我的要求是,一个过程需要通过另一个过程来挑选共享变量的更新值。但是我可以看到,尽管一个过程是将值从0更新为1,但其他过程没有拾取更新的值1,它正在采用旧值0。

我已经通过Internet上的某些解决方案,但无法求解这。请您提供帮助。

注意:根据我的代码逻辑,一旦LifeCycle()方法执行完成,则只会调用GET_LIFECYCLE()方法。 (由于某些限制,我无法粘贴完整代码)

import os
from multiprocessing import Value
... some other imports

data = Value('i', 0)

class ResourceDriverHandler:

    def lifecycle(self , driver_files):  (called by a process - say, process 7)
        try:
            logger.info(f'checkpoint1 os.getpid() :{os.getpid()}')
            logger.info('checkpoint1 data.value : %s : ', data.value)
            .... some other code
            driver.operation(driver_files)
            .... some other code
        except DriverConfigError as e:
            logger.info(f'checkpoint2 os.getpid() :{os.getpid()}')
            data.value += 1
            logger.info('checkpoint2 data.value : %s : ', data.value)
            
    def get_lifecycle(self):    (called by another different process - say, process 5, once lifecycle() method execution is over)
        logger.info(f'checkpoint3 os.getpid() :{os.getpid()}')
        logger.info('checkpoint3 data.value : %s : ', data.value)
        .... some other code
    

Gunicorn WSGI服务器日志:

checkpoint1 os.getpid() : 7
checkpoint1 data.value : 0
checkpoint2 os.getpid() : 7
checkpoint2 data.value : 1
checkpoint3 os.getpid() : 5
checkpoint3 data.value : 0  (I was expecting 1 as already data.value is changed from 0 to 1 by another process)

I am new to Python Multiprocessing. I am deploying my Python code in WSGI Gunicorn Multiprocessing environment. My requirement is, updated value of shared variable by one process needs to be picked up by another process. But I can see though one process is updating the value from 0 to 1 but still other process is not picking up the updated value 1, it is taking old value 0.

I have gone through some of the solution on internet but not able to solve this. Request you to help.

Note: As per my code logic, once lifecycle() method execution gets completed then only get_lifecycle() method will be called. (full code I can't paste due to some restrictions)

import os
from multiprocessing import Value
... some other imports

data = Value('i', 0)

class ResourceDriverHandler:

    def lifecycle(self , driver_files):  (called by a process - say, process 7)
        try:
            logger.info(f'checkpoint1 os.getpid() :{os.getpid()}')
            logger.info('checkpoint1 data.value : %s : ', data.value)
            .... some other code
            driver.operation(driver_files)
            .... some other code
        except DriverConfigError as e:
            logger.info(f'checkpoint2 os.getpid() :{os.getpid()}')
            data.value += 1
            logger.info('checkpoint2 data.value : %s : ', data.value)
            
    def get_lifecycle(self):    (called by another different process - say, process 5, once lifecycle() method execution is over)
        logger.info(f'checkpoint3 os.getpid() :{os.getpid()}')
        logger.info('checkpoint3 data.value : %s : ', data.value)
        .... some other code
    

Gunicorn WSGI server Logs:

checkpoint1 os.getpid() : 7
checkpoint1 data.value : 0
checkpoint2 os.getpid() : 7
checkpoint2 data.value : 1
checkpoint3 os.getpid() : 5
checkpoint3 data.value : 0  (I was expecting 1 as already data.value is changed from 0 to 1 by another process)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文