Single

2020_12_22_Python_线程12.1_随堂

直接创建

from multiprocessing import Process
import os
def do_task():
    print('------子进程运行:%s'%(os.getpid()))

process = Process(target=do_task())

子类创建

from multiprocessing import Process
import time
import os
class MyProcesss(Process):
    def __init__(self,interval):
        Process.__init__(self)
        self.interval = interval

    def run(self):
        time_start = time.time()
        time.sleep(self.interval)
        time_end = time.time()
        print("子线程%s执行结束,耗时%0.2f秒"%(os.getpid(),(time_end--time_start)))

if __name__ == '__main__':
    process = MyProcesss(5)
    process.start()<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>

暂无评论

发表评论