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

added pubsub.run_in_thread as it is implemented in redis-py #214

Merged
merged 5 commits into from
Sep 25, 2018

Conversation

AlexEshoo
Copy link
Contributor

Closes #213

@coveralls
Copy link

coveralls commented Sep 5, 2018

Coverage Status

Coverage decreased (-0.1%) to 98.141% when pulling e3d5326 on AlexEshoo:master into ed36fff on jamesls:master.

@bmerry
Copy link
Collaborator

bmerry commented Sep 8, 2018

Sorry for the delay on this. I haven't used run_in_thread myself, so I need to get around to reading up on what it does. I'm concerned that this might not be thread-safe given that fakeredis in general is not thread-safe at all.

Copy link
Collaborator

@bmerry bmerry left a comment

Choose a reason for hiding this comment

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

This won't be thread safe because the worker thread calls get_message, which in turn manipulates the pubsub object.

I'm planning to do some work to generally improve thread safety, so I'm going to hold off on this PR until I've done that (unfortunately it may take a while to get the time for it). In the meantime I've left some comments with nitpicks.

q.put(msg)

pubsub = self.redis.pubsub()
pubsub.subscribe(**{"channel": lambda e: _queue_msg(e, q)})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of the lambda and _queue_msg, you could just pass q.put as the callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

msg = b"Hello World"
self.redis.publish("channel", msg)

sleep(1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the sleep necessary? q.get should block until the message arrives.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed


pubsub.psubscribe(**{"channel": None})
with self.assertRaises(redis.exceptions.PubSubError):
pubsub_thread = pubsub.run_in_thread()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure this test will be effective, since you'll still have the normal subscribe to the channel so it will be bailing out due to that rather than the psubscribe without handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unsubscribed first.

with self.assertRaises(redis.exceptions.PubSubError):
pubsub_thread = pubsub.run_in_thread()

pubsub.psubscribe(**{"channel": None})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than constructing a dict then **ing it, just pass channel=None.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@bmerry
Copy link
Collaborator

bmerry commented Sep 19, 2018

I've just noticed that redis-py says "It is not safe to pass PubSub or Pipeline objects between threads." So I'm no longer worried about thread safety, and once my other comments are addressed I'll be happy to merge this.

It also looks like FakePubSubWorkerThread is identical to the redis PubSubWorkerThread (by eye - I didn't diff them). In that case, we could save some code by just importing PubSubWorkerThread.

@AlexEshoo
Copy link
Contributor Author

AlexEshoo commented Sep 19, 2018

I agree passing the pubsub objects between threads is not safe, it should be up to the user to decide if what they are doing is safe to do, but in many cases subscriptions can be responded to in a separate thread without needing to modify the pubsub object.

And yes it is a carbon copy other than the name, so importing from redis-py should be fine.

I'll see if I can make some edits later this week.

@bmerry
Copy link
Collaborator

bmerry commented Sep 25, 2018

Thanks for the updates. It all looks good, but just needs a fix for a merge conflict with some thread-safety improvements I merged this morning.

@bmerry bmerry merged commit 3a4966c into jamesls:master Sep 25, 2018
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.

None yet

3 participants