[Python] Python multiprocessing: How to release an unknown lock?

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    This is a question about multiprocessing execution getting stuck in combindation with a third-party package (suppose we call it package). Let's pretend this is my code

    def work(arg):
    from package import f
    return f(arg)

    from multiprocessing import Pool
    pool = Pool(8)
    args = [ i for i in range(80) ]
    results = pool.map(work, args)
    for result in results:
    result
    pool.close()


    In my particular case, this code runs fine until I use other functionality from package. If I modify the script like so:

    def work(arg):
    from package import f
    return f(arg)

    from package import SomeClass
    obj = SomeClass()

    from multiprocessing import Pool
    pool = Pool(8)
    args = [ i for i in range(80) ]
    results = pool.map(work, args) # execution gets stuck on this line
    for result in results:
    result
    pool.close()


    then it gets stuck on the commented line. Since this is a third-party package, I have imperfect knowledge of its inner workings, but I see no reason (using a reasonable amount of time) that SomeClass should depend on what happens in work. Since I don't need the object obj when I get to the multiprocessing, I want to delete it (or otherwise render ineffective its interaction with f) hoping that the map can execute. But it doesn't help

    def work(arg):
    from package import f
    return f(arg)

    from package import SomeClass
    obj = SomeClass()
    obj = None
    del obj
    import gc
    gc.collect()

    from multiprocessing import Pool
    pool = Pool(8)
    args = [ i for i in range(80) ]
    results = pool.map(work, args) # execution still gets stuck on this line
    for result in results:
    result
    pool.close()

    • Is there something I can do in reasonable time to find out what is causing map execution to get blocked?
    • I want to use brute force to get rid of the lock (or whatever it is in SomeClass preventing execution) after am done with obj. What are my options to achieve that?

    Edit: I noticed that I can get it to execute but not in parallel. So this worked:

    def work(arg):
    from package import f
    return f(arg)

    from package import SomeClass
    obj = SomeClass()
    obj = None
    del obj
    import gc
    gc.collect()

    from multiprocessing import Pool
    pool = Pool(8)
    arg = 42
    work(arg) # execution work if I don't run it in parallel

    Continue reading...

Compartilhe esta Página