方式一:
from multiprocessing import Process
import time
def task(x):
print('%s is running' % x)
time.sleep(5)
print('%s is done' % x)
if __name__ == '__main__':
p = Process(target=task, args=('子进程',))
p.start()
print('主')
方式二:
from multiprocessing import Process
import time
class MyProcess(Process):
def __init__(self,name):
super().__init__()
self.name = name
def run(self):
print('%s is running'%self.name)
time.sleep(5)
print('%s is over'%self.name)
if __name__ == '__main__':
p = MyProcess('json')
p.start()
print('主')