Skip to content

Instantly share code, notes, and snippets.

@jimmy-ly00
Created June 18, 2017 17:41
Show Gist options
  • Save jimmy-ly00/1515b3a9331d0b556cc2e126d4a515b2 to your computer and use it in GitHub Desktop.
Save jimmy-ly00/1515b3a9331d0b556cc2e126d4a515b2 to your computer and use it in GitHub Desktop.
Gevent thread pool example of mixing
#!/usr/bin/python
import random
from math import log, exp
import string
from gevent import sleep, spawn, joinall, wait
from gevent.threadpool import ThreadPool
def random_string():
digits = "".join( [random.choice(string.digits) for i in xrange(8)] )
chars = "".join( [random.choice(string.letters) for i in xrange(10)] )
return chars
def delay_message(message, delay):
sleep(delay)
print("Shuffled message: {} and time: {}". format(message, delay))
def get_delay():
u = random.SystemRandom().random()
tau = 10 # maximum bound
x = -log(1 - (1 - exp(-tau)) * u);
print(u)
def main():
tasks = ThreadPool(25)
while True:
s = raw_input("Please continue pressing enter, messages will appear when they are ready")
if s == "":
delay = random.randint(0, 6)
string = random_string()
print("Message: {} and time: {}". format(string, delay))
#tasks = []
tasks.spawn(delay_message, string, delay)
if (random.randint(0,10) == 5): # random condition in breaking
wait()
break
else:
print("Exiting")
break
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment