In the introduction of multithreading, it is introduced that you can customize the multithreaded class and rewrite the run method of the multithreaded class to realize the second way of using multithreading.
In multi-process, you can also customize the multi-process class by rewriting the run method of the multi-process class
import multiprocessing
class MyProcess(multiprocessing.Process):
def __init__(self, func, args):
# Encapsulate function names and parameters into this class
self.func = func
self.args = args
# In order to ensure the normal operation of the inherited class, the construction method of the parent class needs to be executed here
# But no parameters are passed to the constructor of the parent class
# When the object looks for parameters, it is also the first to look for this class
super(MyProcess, self).__init__()
def run(self):
# Execute the function in the parameter
self.func(*self.args)
# Customize a function that needs the child process to run
def f(*args):
print(args)
# Use custom process object execution
p = MyProcess(func=f, args=(59,15))
p.start()
When process p calls start(), run() is automatically called