Skip to content

Instantly share code, notes, and snippets.

@flushentitypacket
Created July 18, 2020 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flushentitypacket/337731215a4dd7c15b5a37a293644c56 to your computer and use it in GitHub Desktop.
Save flushentitypacket/337731215a4dd7c15b5a37a293644c56 to your computer and use it in GitHub Desktop.
Difference between python thread vs gevent greenlet
#!/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])
#!/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