why cannot cuda model be initialized under the __init__ method in a class that inherits multiprocessing.process?
Here is my code:

from MyDetector import Helmet_Detector
from multiprocessing import Process

class Processor(Process):
    def __init__(self):
        super().__init__()
        self.helmet_detector = Helmet_Detector()

    def run(self):
         print(111)


    if __name__ == '__main__':
         p=Processor()
         p.start()


As you can see, the class 'Processor' inherits multiprocessing.Process, and Helmet_Detector is a YOLO model using cuda. But when I ran it, the error occurred as follow:

THCudaCheck FAIL file=C:\w\1\s\tmp_conda_3.7_075911\conda\conda-bld\pytorch_1579075223148\work\torch/csrc/generic/StorageSharing.cpp line=245 error=71 : operation not supported
Traceback (most recent call last):
  File "E:/python-tasks/WHU-CSTECH/Processor.py", line 17, in &lt;module&gt;
    p.start()
  File "C:\Anaconda\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Anaconda\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Anaconda\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Anaconda\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Anaconda\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:\Anaconda\lib\site-packages\torch\multiprocessing\reductions.py", line 242, in reduce_tensor
    event_sync_required) = storage._share_cuda_()
RuntimeError: cuda runtime error (71) : operation not supported at C:\w\1\s\tmp_conda_3.7_075911\conda\conda-bld\pytorch_1579075223148\work\torch/csrc/generic/StorageSharing.cpp:245


then I tried to intialize the Helmet_Detector in run method:

def run(self):
     print(111)
     self.helmet_detector = Helmet_Detector()


No error occurred. Could anyone please tell me the reason for this and how could I solve this problem? Thank you!

Error occurs because in python multiprocessing requires Process class objects to be pickelable so that data can be transferred to the process being created i.e. Serialisation and deserialization of the object. Suggestion to overcome the issue, lazy instantiate the Helmet_Detector object (hint: try property in python).

Edit:

As per the comment by @jodag, you should use pytorch's multiprocessing library instead of standard multiprocessing library

Example:

import torch.multiprocessing as mp

class Processor(mp.Process):
.
.
.

