共享变量不使用Python中的多处理值
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论