Skip to content

Make multiprocessing.Queue a subclass of queue.Queue #1525

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

Merged
merged 1 commit into from
Aug 8, 2017

Conversation

bcl
Copy link
Contributor

@bcl bcl commented Aug 6, 2017

Also change multiprocessing.Queue's put and get timeout arguments to
allow None.

This fixes a problem with logging.handlers.QueueHandler and
QueueListener not accepting a multiprocessing.Queue as the queue
argument.

@@ -93,10 +94,10 @@ class Process():
def is_alive(self) -> bool: ...
def join(self, timeout: Optional[float] = ...) -> None: ...

class Queue():
class Queue(queue.Queue):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this implicitly makes the base class queue.Queue[Any]. I wonder if it wouldn't be better to make this Queue class generic as well, so one can instantiate a mp.Queue[int], say.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not that familiar with the mypy types yet, so I'm not sure if I'm doing this correctly, but changing it to a Generic[_T] like queue.Queue gives me error: Need type annotation for variable when I do q = mp.Queue()

@gvanrossum
Copy link
Member

Hm, that's also the case when instantiating queue.Queue() so maybe this is a feature? I'm reluctant to keep mp.Queue non-generic. Take the example from the docs, the type of q should probably be declared as Queue[List[Any]], so that the type-checker will know that q.get() returns a List.

@bcl bcl force-pushed the master-multiprocessing.Queue branch from ce4f291 to 9488b31 Compare August 6, 2017 15:02
@bcl
Copy link
Contributor Author

bcl commented Aug 6, 2017

Ah! Right, that makes sense.

@bcl bcl force-pushed the master-multiprocessing.Queue branch from 9488b31 to 27ee043 Compare August 6, 2017 15:51
@bcl
Copy link
Contributor Author

bcl commented Aug 6, 2017

I'm not sure what's up with that travis failure. When I run the tests here they all pass:

SUMMARY  all 151 tasks and 1451 tests passed                                    
*** OK ***
total runtime: 166.2158714469988 sec
mypy tests succeeded

@gvanrossum
Copy link
Member

I've restarted the test job. I believe it's an intermittent Travis failure: python/mypy#3543.

def get(self, block: bool = ..., timeout: float = ...) -> Any: ...
def put(self, item: Any, block: bool = ..., timeout: float = ...) -> None: ...
def get(self, block: bool = ..., timeout: Optional[float] = ...) -> Any: ...
def put(self, item: Any, block: bool = ..., timeout: Optional[float] = ...) -> None: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two Anys in get() and put() should be replaced with _T, to match the same methods in queue.Queue.

Also change multiprocessing.Queue's put and get timeout arguments to
allow None.

This fixes a problem with logging.handlers.QueueHandler and
QueueListener not accepting a multiprocessing.Queue as the queue
argument.

Declaring the Queue now needs to note what it will be used for. eg.
q = multiprocessing.Queue()    # type: multiprocessing.Queue[List[Any]]
@bcl bcl force-pushed the master-multiprocessing.Queue branch from 27ee043 to 16843f6 Compare August 7, 2017 00:59
@bcl
Copy link
Contributor Author

bcl commented Aug 7, 2017

Oops, sorry about that. Also changed Any in put_nowait and get_nowait to _T

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I'll let Guido merge.

@gvanrossum gvanrossum merged commit 19275ea into python:master Aug 8, 2017
@gvanrossum
Copy link
Member

Thanks! I am still a little worried that this may break code that's using this without an annotation; we'll see how it goes in practice.

@gvanrossum
Copy link
Member

Oh, PS. Next time please don't squash your local commits -- this makes it harder to track a code review, and it's not needed, since we already squash when we merge into master.

@bcl
Copy link
Contributor Author

bcl commented Aug 8, 2017

Will do. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants