Python多进程

# -*- coding: UTF-8 -*-

# import os

# import time

# print('Process(%s)start...'%os.getpid()) #输出当前进程

# while(1):

#    #time.sleep(0.5)

#    pass

# pid = os.fork() #fork一个子进程

# if pid == 0:

#    print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))

# else:

#    print('I (%s) just created a child process (%s).' % (os.getpid(), pid))

from multiprocessing import Process

import os

import time

# 子进程要执行的代码

def run_proc(name):

    progress=0

    while(1):

        progress+=1

        time.sleep(2.)

        print('Run child process %s--%s--父进程:%s (%s)...' % (name,progress,os.getppid(),os.getpid()))

if __name__=='__main__':

    print('Parent process %s.' % os.getpid())

    p = Process(target=run_proc, args=('test',))

  # p.daemon = True #daemo守护线程,

    print('Child process will start.')

    p.start()

  #  p.join() #join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步。

    print('Child process end.')

你可能感兴趣的:(Python多进程)