zoukankan      html  css  js  c++  java
  • multiprocessing跨平台锁的使用(Windows问题)

    在Windows上可能遇到,开启的子进程不会关闭的问题

    参考multiprocessing官方文档:

    Explicitly pass resources to child processes

    On Unix a child process can make use of a shared resource created in a parent process using a global resource. However, it is better to pass the object as an argument to the constructor for the child process.

    Apart from making the code (potentially) compatible with Windows this also ensures that as long as the child process is still alive the object will not be garbage collected in the parent process. This might be important if some resource is freed when the object is garbage collected in the parent process.

    So for instance

    from multiprocessing import Process, Lock
    
    def f():
        ... do something using "lock" ...
    
    if __name__ == '__main__':
        lock = Lock()
        for i in range(10):
            Process(target=f).start()
    

    should be rewritten as

    from multiprocessing import Process, Lock
    
    def f(l):
        ... do something using "l" ...
    
    if __name__ == '__main__':
        lock = Lock()
        for i in range(10):
            Process(target=f, args=(lock,)).start()
  • 相关阅读:
    yarn 0.9.0 build spark
    redhat6.4上build storm 0.9.0.1
    redhat6.4安装storm集群-4节点
    Hadoop 2.2.0 4结点集群安装 非HA
    redhat6.4上安装mysql
    redhat6.4上用apache建立os repos
    Hive Over HBase
    Hadoop 2.2.0学习笔记20131210
    Hadoop 2.2.0学习笔记20131209
    IDH2.5.1. Pain Points
  • 原文地址:https://www.cnblogs.com/playboysnow/p/6208764.html
Copyright © 2011-2022 走看看