Slow multithreading
See original GitHub issueFrom my understanding, Redis-py is mostly doing network IO under the hood. Python’s GIL should not kick in for network IO. However, it looks like multithreading is as slow as single-threaded sequential version. Am I missing anything?
r = StrictRedis()
def test():
for i in range(10000):
key = 'mykey'+str(i)
r.set(key, 'ibvjaiuj65s4fa4sd8ear4')
r.get(key)
r.delete(key)
def bench1():
for _ in range(5):
test()
def bench2(): # as slow as bench 1
ts = [threading.Thread(target=test) for i in range(5)]
[t.start() for t in ts]
[t.join() for t in ts]
def bench3(): # much faster, which means GIL is in play
ts = [multiprocessing.Process(target=test) for i in range(5)]
[t.start() for t in ts]
[t.join() for t in ts]
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Why is multithreaded slower? - c++ - Stack Overflow
I'm fairly sure cout acts a shared resource - and even if it actually prints each number correctly and in the right order,...
Read more >Why is multi-threaded Python so slow? - Dev Genius
The test results show multi-threaded code is indeed significantly slower compared to multi-process code or even serialised execution. Surprisingly the baseline ...
Read more >Multi-threading slower than expected from single-thread loop
It shows the multi-threading has no benefit at all. I am not even using mutexes but just multiple threads. Compiler / IDE Used:...
Read more >C++: why my multi-threads program is slower than single ...
I decide to go with multithreading. I created a few threads and each thread will handle a portion of the vector, store its...
Read more >Need faster code? Try Multithreading - Medium
In fact, multithreading can be slower due to the overhead of creating the threads and context switching between them.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@LinxiFan GIL is always invoked whenever where is thread. GIL grantee there is only one thread running in Python interpreter.
You should noticed that redis in running on memory, so if you test your code in localhost, there is no networking io or file system io.
Compare to single thread, multi thread need switching(which cost), so slower than single thread.
@LinxiFan Multithreading and concurency in python is very hard and i would suggest that you dig into the talks from David Beazley has done on this topic to better understand the problem with threads/multiprocessing/async and so on. One of the better presentations he has done is this one https://www.youtube.com/watch?v=MCs5OvhV9S4 where he show code examples live and the performance of the different methods.