python_basic_10 (零基础---socket基本使用)

适合零基础学习的—关于多任务-进程的简单练习:

import multiprocessing
import time
import os
    
"""
获取pid
"""
def test1():
    while True:
        print("--in 子进程 pid={}".format(os.getpid()))
        time.sleep(1)


def main():
    print("--in 父进程 pid = %d" % os.getpid())
    p = multiprocessing.Process(target=test1)
    p.start()

    p2 = multiprocessing.Process(target=test2)
    p2.start()


if __name__ == '__main__':
    main()


"""
获取pid以及ppid, 运行时间不确定
"""

def test1():
    while True:
        print("--in 子进程 pid={} 父进程的ppid={} ".format(os.getpid(),os.getppid()))
        time.sleep(1)

def test2():
    while True:
        print("--in 子进程2 pid={} 父进程的ppid={} ".format(os.getpid(),os.getppid()))
        time.sleep(1)

def main():
    print("--in 主进程 pid=%d 父进程 ppid = %d" % (os.getpid(),os.getppid()))
    p = multiprocessing.Process(target=test1)
    p.start()

    p2 = multiprocessing.Process(target=test2)
    p2.start()

if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/weixin_44786482/article/details/89873631
今日推荐