6

I'm using Python multiprocessing Process, Manager (dict). I want to run this script:

from time import sleep


from multiprocessing import Process
from multiprocessing import Queue, Value, Array
from multiprocessing import Manager


def main(id_, graf_dict):
    print('Граф {} готов'.format(id_))
    graf_dict[id_] = 1
    if id_ == '3':
        graf_dict[id_] = 0
        print(graf_dict)
        while True:
            check = 0
            for key in graf_dict:
                if graf_dict[key] == 0:
                    check = 1
                    break

            if check == 0:
                print('Все графы авторизованы')
                break


if __name__ == "__main__":
    manager = Manager()
    graf_control = manager.dict()
    graf_control['1'] = 0
    graf_control['2'] = 0
    graf_control['3'] = 0
    print(graf_control)

    p1 = Process(target=main, args=(str(1), graf_control,))
    p2 = Process(target=main, args=(str(2),graf_control,))
    p3 = Process(target=main, args=(str(3),graf_control,))

    p1.start()
    sleep(1)
    p2.start()
    sleep(1)
    p3.start()

    p1.join()
    p2.join()
    p3.join()

But I got an error:

AttributeError: 'NoneType' object has no attribute '_registry'

I didn't find a solution to this error and I need help to get the code running. Are there any ways to do this?

4
  • 3
    Please provide the full stack trace. Commented Feb 6, 2020 at 18:26
  • 4
    Use graf_dict.keys() for iterating over the manager-dict in a child process. Commented Feb 6, 2020 at 21:47
  • @Darkonaut I think you are right. Commented May 10, 2020 at 16:02
  • Also, the logic of your code seems to be wrong. Particularly, when _id = 3, the infinite loop is run, graf_dict['3'] is set to 0. Because you are checking for whether any values in graf_dict are equal to 0, the loop will run forever until you break it. Commented Oct 4, 2020 at 9:04

1 Answer 1

4

As per @Darkonaut comment, use graf_dict.keys() like so:

...
for key in graf_dict.keys():
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.