Created
July 18, 2020 18:55
-
-
Save flushentitypacket/337731215a4dd7c15b5a37a293644c56 to your computer and use it in GitHub Desktop.
Difference between python thread vs gevent greenlet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Output is NOT interleaved | |
import gevent | |
def print_numbers(name): | |
count = 0 | |
while count < 500: | |
count += 1 | |
print("Thread: {}, count: {}".format(name, count)) | |
g1 = gevent.spawn(print_numbers, "Thread1") | |
g2 = gevent.spawn(print_numbers, "Thread2") | |
gevent.joinall([g1, g2]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Output will be interleaved with Thread-1 and Thread-2 | |
import thread | |
def print_numbers(name): | |
count = 0 | |
while count < 500: | |
count += 1 | |
print("Thread: {}, count: {}".format(name, count)) | |
thread.start_new_thread(print_numbers, ("Thread-1",)) | |
thread.start_new_thread(print_numbers, ("Thread-2",)) | |
while 1: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment