为什么在使用多进程时,我的全局列表没有被附加在Python中?

huangapple go评论54阅读模式
英文:

Why is my global list not getting appended in Python when using multiprocessing?

问题

当我运行代码时,全局列表保持为空,尽管它应该得到条目。
测试未附加,这可能是由于多进程吗?

import multiprocessing
import os
import time

uncounted = []
test = [{'id': 'UC8YCf2zyqY8E7-CCOnr_kQA', 'name': 'dominic alting:('}, {'id': 'UCxm1n3Hil3l9mmPWcONd4Dg', 'name': 'Toes Jr.'}]

dir_path = "./json/"
i = 0
xi = -1

def load(pathD):
    pathD = test
    global uncounted
    return pathD

def multiprocessing_func(dp):
    global uncounted
    time.sleep(2)
    u = load(dp)
    uncounted.append(u)

errors = []

def main(xi):
    if __name__ == '__main__':
        starttime = time.time()
        processes = []
        for i in range(0, 10):
            print(f'{i} has started')
            i += 1
            xi += 1
            try:
                p = multiprocessing.Process(
                    target=multiprocessing_func, args=(os.listdir(dir_path)[xi],))
                processes.append(p)
                p.start()
            except:
                errors.append(i)
                print(f"{i} - no more path")

        for process in processes:
            process.join()
        print('That took {} seconds'.format(time.time() - starttime))
        print(uncounted)

main(xi)

尝试过不使用全局变量进行附加,但结果相同。

英文:

When I run my code the global list stays empty, even though it should get the entries.
The test doesn't get appended, is it likely due to multi processing?

import multiprocessing
import os
import time

uncounted = []
test = [{'id': 'UC8YCf2zyqY8E7-CCOnr_kQA', 'name': 'dominic alting:('}, {'id': 'UCxm1n3Hil3l9mmPWcONd4Dg', 'name': 'Toes Jr.'}]

dir_path = "./json/"
i = 0
xi = -1


def load(pathD):
    pathD = test
    global uncounted
    return pathD


def multiprocessing_func(dp):
    global uncounted
    time.sleep(2)
    u = load(dp)
    uncounted.append(u)


errors = []


def main(xi):
    if __name__ == '__main__':
        starttime = time.time()
        processes = []
        for i in range(0, 10):
            print(f'{i} has started')
            i += 1
            xi += 1
            try:
                p = multiprocessing.Process(
                    target=multiprocessing_func, args=(os.listdir(dir_path)[xi],))
                processes.append(p)
                p.start()
            except:
                errors.append(i)
                print(f"{i} - no more path")

        for process in processes:
            process.join()
        print('That took {} seconds'.format(time.time() - starttime))
        print(uncounted)

main(xi)

Tried appending without global but then I get the same outcome

答案1

得分: 0

更改列表的函数因没有文件而未运行,这必须始终进行审查。

英文:

The function, that changes the list, isn't running, because there is no file. This must be always reviewed.

huangapple
  • 本文由 发表于 2023年5月24日 18:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定