zoukankan      html  css  js  c++  java
  • 7阻塞进程/ 线程的子进程/子线程之方法jion()

    """阻塞进程的子进程之方法jion()"""


    """
    在父进程中创建并启动子进程后,可以调用子进程的方法jion(),这样子进程就会把父进程
    阻塞。父进程等子进程执行完后再被阻塞的地方继续执行。
    在调用方法jion()时,可以指定参数timeout,设置阻塞时间
    """

    from multiprocessing import current_process, Process
    import time

    class MyProcess(Process):
    def run(self):
    print(('子进程 %d启动' % current_process().pid))
    time.sleep(2)
    print(('子进程 %d结束' % current_process().pid))


    def main():
    print(('父进程 %d启动' % current_process().pid))

    mp = MyProcess()
    mp.start()

    # 阻塞
    # mp.join()
    # 阻塞设定时间
    mp.join(1)
    #time.sleep(5)

    print(('父进程 %d结束' % current_process().pid))

    if __name__ == '__main__':
    main()

    #############################################################################

    """阻塞线程的子线程之方法jion()"""


    """
    在父线程中创建并启动子线程后,可以调用子线程的方法jion(),这样子线程就会把父线程
    阻塞。父线程等子线程执行完后再被阻塞的地方继续执行。
    在调用方法jion()时,可以指定参数timeout,设置阻塞时间
    """

    from threading import current_thread, Thread
    import time

    class Mythread(Thread):
    def run(self):
    print(('子线程 %s启动' % current_thread().getName()))
    time.sleep(2)
    print(('子线程 %s结束' % current_thread().getName()))


    print(('父线程 %s启动' % current_thread().getName()))

    mt = Mythread()
    mt.start()

    # 阻塞
    #mt.join()
    # 阻塞设定时间
    mt.join(3)

    print(('父线程 %s结束' % current_thread().getName()))
  • 相关阅读:
    用pyenv 和 virtualenv 搭建单机多版本python 虚拟开发环境
    如何快速地编写和运行一个属于自己的 MapReduce 例子程序
    Hive如何加载和导入HBase的数据
    kettle中的karaf设置
    sqoop学习
    windows 本地配置hadoop客户端
    python 随机分类
    python 皮尔森相关系数
    kettle配置命名参数
    idea配置scala和spark
  • 原文地址:https://www.cnblogs.com/sruzzg/p/12993940.html
Copyright © 2011-2022 走看看