A
A
agent_22032021-09-14 19:49:41
Python
agent_2203, 2021-09-14 19:49:41

How to add multiprocessing.Process to multiprocessing.Manager().dict()?

When trying to add a newly created multiprocessing.Process to multiprocessing.Manager().dict() , the following error occurs

Traceback (most recent call last):
  File "main.py", line 36, in <module>
    start_old_tasks.start_old_tasks(manager_process_task)
  File "/root/subserver/start_old_tasks.py", line 21, in start_old_tasks
    manager_process_task.continue_task(
  File "/root/subserver/cheating/manager_process_task.py", line 186, in continue_task
    task_processes_.update({uuid: [prcs, "simple_vote"]})
  File "<string>", line 2, in update
  File "/usr/lib/python3.8/multiprocessing/managers.py", line 834, in _callmethod
    conn.send((self._id, methodname, args, kwds))
  File "/usr/lib/python3.8/multiprocessing/connection.py", line 206, in send
    self._send_bytes(_ForkingPickler.dumps(obj))
  File "/usr/lib/python3.8/multiprocessing/reduction.py", line 51, in dumps
    cls(buf, protocol).dump(obj)
TypeError: cannot pickle 'weakref' object


Here is the code

task_processes = Manager().dict()
prcs = Process(
    target=start_prcss__simple_vote_start, 
    args=[
        uuid, data_info, 
        sql_request, logger
    ], 
    daemon=True
)
prcs.start()
task_processes.update({uuid: [prcs, "simple_vote"]})


Also, to bypass the "protection" of processes, I redefined the Process class

class Process(Process):
    def __init__(self, **args):
        super(Process, self).__init__(**args)
        print("Object", self, "created.")

    def run(self):
        print("Object", self, "process started.")

    def __getstate__(self):
        state = self.__dict__.copy()
        conf = state['_config']
        if 'authkey' in conf: 
            conf['authkey'] = bytes(conf['authkey'])
        return state

    def __setstate__(self, state):
        state['_config']['authkey'] = AuthenticationString(state['_config']['authkey'])
        self.__dict__.update(state)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question