Using defaultdict with multiprocessing?

后端 未结 2 928
天命终不由人
天命终不由人 2021-02-04 13:21

Just experimenting and learning, and I know how to create a shared dictionary that can be accessed with multiple proceses but I\'m not sure how to keep the dict synced. de

2条回答
  •  灰色年华
    2021-02-04 13:58

    Well, the Manager class seems to supply only a fixed number of predefined data structures which can be shared among processes, and defaultdict is not among them. If you really just need that one defaultdict, the easiest solution would be to implement the defaulting behavior on your own:

    def test(k, multi_dict):
        if k not in multi_dict:
            multi_dict[k] = 0
        multi_dict[k] += 1
    

提交回复
热议问题