zoukankan      html  css  js  c++  java
  • Python学习笔记24:多进程

    #多进程
    ——进程间通讯(IPC:InterProcessCommunication)
    ——进程之间无任何共享状态



    import multiprocessing
    from time import sleep, ctime
    class ClockProcess(multiprocessing.Process):
    def __init__(self, interval):
    super().__init__()
    self.interval = interval

    def run(self):
    while True:
    print('The time is %s'%ctime())
    sleep(self, interval)

    if __name__ == '__main__':
    p = ClockProcess(3)
    p.start()
    while True:
    print('sleeping......')
    sleep(1)


    sleeping......
    Process ClockProcess-1:
    Traceback (most recent call last):
    File "C:UsersBruceAppDataLocalProgramsPythonPython37libmultiprocessingprocess.py", line 297, in _bootstrap
    self.run()
    File "C:迅雷下载demo.py", line 81, in run
    sleep(self, interval)
    NameError: name 'interval' is not defined
    The time is Mon Dec 24 23:38:25 2018
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......
    sleeping......

    Process finished with exit code -1

    from multiprocessing import Process
    import os
    def info(title):
    print(title)
    print('module name:', __name__)
    #得到父进程的id
    print('parent process:', os.getppid())
    #得到本身进程的id
    print('process id:', os.getpid())

    def f(name):
    info('function f')
    print('hello', name)

    if __name__ == '__main__':
    info('main line')
    p = Process(target = f, args = ('bob',))
    p.start()
    p.join()

    main line
    module name: __main__
    parent process: 94984
    process id: 103092
    function f
    module name: __mp_main__
    parent process: 103092
    process id: 21428
    hello bob

    Process finished with exit code 0

  • 相关阅读:
    CSS媒体查询
    搜索关键词标注红色
    揭秘 | 小白如何0基础0元建站
    细说浏览器输入URL后发生了什么
    js问题总结
    vue elementui如何修改el-table头部样式
    h5开发微信公众号重定向到关注页面没有关注按钮 (微信你个坑)
    下拉展开动画
    html中常用的转义字符总结
    9个设计师常用的高清图库 不敢配图? 这9个免版权图库牢记心中!
  • 原文地址:https://www.cnblogs.com/chickenwrap/p/10171773.html
Copyright © 2011-2022 走看看