Domanda

I have the following script:

from multiprocessing import Lock, Pool

def worker():
    r = other.work()
    return r

def main():
    pool = Pool(4)
    result = pool.apply_sync(worker,())
    result.wait()

In worker(), I call function work() from another module 'other', However, I forget to import the module 'other'. But when I ran this python script, python didn't report any Exception. Is this a bug ?

È stato utile?

Soluzione

Any errors that are raised in the spawned processes will remain silent until you actually retrieve the result.

from multiprocessing import Lock, Pool

def worker():
    r = other.work()
    return r

def main():
    pool = Pool(4)

    # note: this is apply_async, not apply_sync
    result = pool.apply_async(worker,())
    result.wait()

    # See here
    actual_result = result.get()

This will raise:

NameError: global name 'other' is not defined

What you have named result is a multiprocessing.pool.ApplyResult object which is more like the promise of a return value, than a return value itself.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top