英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论