Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected python bug using multiprocessing and inheritance #96469

Open
bsdis opened this issue Sep 1, 2022 · 1 comment
Open

Expected python bug using multiprocessing and inheritance #96469

bsdis opened this issue Sep 1, 2022 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@bsdis
Copy link

bsdis commented Sep 1, 2022

This is a bug report:

import time
import multiprocessing as mp
class A(object):
    def __init__(self, childnodes, **kwargs) -> None:
        self._childnodes = childnodes
        self._kwargs = kwargs

    def process(self, datablock, meta):
        print("Hello")
        time.sleep(1)
        print("world")

class B(A):
    def process(self, datablock, meta):
        print("before")
        mp.Process(target=super().process, args=(self, datablock, meta)).start()
        print("after")

if __name__ == "__main__":
    b = B([1,2,3])
    b.process([4,5,6], {'foo':42})

This seems to run forerever which is not expected. Reason seems to be that that somehow, super().process actually calls B.process instead of A.process in the subprocess. This is not expected behaviour. As additional evidence towards that, the program behaviour can be corrected by changing

mp.Process(target=super().process, args=(self, datablock, meta)).start()`meta)).start()

with

mp.Process(target=A.process, args=(self, datablock, meta)).start()

Machine:
MacBook Pro (16-inch, 2021)
MacOS Monteray 12.5
Chip: Apple M1 Max
Python version: 3.9.12

@bsdis bsdis added the type-bug An unexpected behavior, bug, or error label Sep 1, 2022
@saito828koki
Copy link

saito828koki commented Sep 5, 2022

@bsdis

Hello,
Thanks for your interesting bug report!

To summarize that unexpected behavior, super().process method is somehow overridden when calling
multiprocessing.Process.start().
I found that we can get the expected result by using multiprocessing.Process.run().

import multiprocessing as mp


class A(object):
    def process(self):
        print("method of A")


class B(A):
    def foo(self):
        mp.Process(target=super().process).run()  # "method of A" is printed
        mp.Process(target=super().process).start()  # "method of B" is printed

    def process(self):
        print("method of B")


if __name__ == "__main__":
    b = B()
    b.foo()

I will dive deeper to find what makes that difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants