欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python中全局变量的修改

程序员文章站 2024-01-22 19:44:04
...

1.单进程中

a = 1
def func():
    global a
    b = a +1
    a = b
    print(a)
if __name__ == '__main__':
    for i in range(10):
       fun()

2.多进程中
 

import multiprocessing

total_doc_count = multiprocessing.Value("d", 0)

def fun():
    new_count = total_doc_count.value + 1
    total_doc_count.value = new_count
    print(total_doc_count.value)

if __name__ == '__main__': 
    for i in range(10):
        fun()

参考链接:https://blog.csdn.net/jackliu16/article/details/82598298