question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pubsub.run_in_thread : 'sleep_time' and documentation

See original GitHub issue

The pubsub.run_in_thread implementation works great for us, very nice work indeed.

We do however have a question about the parameter name ‘sleep_time’ plus its default value, and about the documentation.

Last week, we had a production issue, which was entirely our fault - not tested thoroughly enough. An example from the documentation had been taken too literally, and our CPU cost estimate was wrong.

Note: where I say ‘documentation’, it may fall under your control or maybe not. We also collected info from StackOverflow and such, we are not entirely sure where the examples came from 😉

We already solved all issues, but recommend the following:

Change sleep_time to socket_timeout. The name sleep_time was very confusing to us, since it’s no sleep at all. It’s used as a TCP timeout value in the select() statement, where control is returned to the OS.

It’s initial value is 0. This is very CPU unfriendly. The documentation says ‘be nice to the CPU’ by setting it to 0.001. This is a tiny bit more nice than 0, but turned out to be not nice enough for our scenario. On a live server, the run_in_thread was running in hundreds of processes on one server, each causing 0.5-1.0% CPU, clogging everything. Revert and reboot needed (and alas, an hour of production downtime due to dependencies).

The initial value for sleep_time (socket_timeout) should be None in our opinion. Since this week, we run the pubsub as a daemon thread with sleep_time=None, and after initialization, this takes 0.0% CPU. It hands over control to the OS. Our hundreds of processes on that live server run fine now.

Caveat: You should not try to use this thread for pubsub connections to more than 1 redis instance. It’s better to fire up an extra thread to accomplish that. Also see the python docs on select: https://docs.python.org/2/howto/sockets.html . Excerpt: give it a nice long timeout (say a minute) unless you have good reason to do otherwise

Kind regards, TW

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:6
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
andymccurdycommented, Jul 23, 2020

Thanks for the feedback. I’ll add this to the redis-py 4.0 TODO list. FYI, 4.0 will be Python 3.5+ only.

0reactions
chayimcommented, Sep 1, 2022

@sav-norem. Wanna?

Read more comments on GitHub >

github_iconTop Results From Across the Web

pubsub.run_in_thread : 'sleep_time' and documentation #821
On a live server, the run_in_thread was running in hundreds of processes on one server, each causing 0.5-1.0% CPU, clogging everything. Revert ...
Read more >
Publish / Subscribe — aredis 1.0.7 documentation
aredis includes a PubSub object that subscribes to channels and listens for ... my_handler}) thread = p.run_in_thread(sleep_time=0.001) # the event loop is ...
Read more >
PubsubMessage | Cloud Pub/Sub Documentation
A message that is published by publishers and consumed by subscribers. The message must contain either a non-empty data field or at least...
Read more >
Is non-blocking Redis pubsub possible? - python
run_in_thread () takes an optional sleep_time argument. If specified, the event loop will call time.sleep() with the value in each iteration of ...
Read more >
google_cloud_pubsub - Rust - Docs.rs
google-cloud-pubsub. Google Cloud Platform pub/sub library. About Cloud Pub/Sub · Pub/Sub API Documentation. Quick Start. Publish ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found