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.

Slow multithreading

See original GitHub issue

From 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:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
laixintaocommented, Dec 22, 2017

@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.

1reaction
Grokzencommented, Dec 3, 2017

@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.

Read more comments on GitHub >

github_iconTop 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 >

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