Skip to content

Instantly share code, notes, and snippets.

# from http://sdiehl.github.io/gevent-tutorial/
from gevent import sleep
from gevent.pool import Pool
from gevent.coros import BoundedSemaphore
sem = BoundedSemaphore(4) # only allows 4 greenlets at one time, others must wait until one is released
def worker1(n):
sem.acquire()
@pydong
pydong / sync_vs_async_gevent.py
Last active January 1, 2016 05:58
Comparison between gevent synchronous calls and asynchronous calls 1) synchronous() # next task is blocked until current one is finished 2) asynchronous() #all start @ the same time
# based on http://sdiehl.github.io/gevent-tutorial/
import gevent
import random
start = time.time()
tic = lambda: 'at %1.1f seconds' % (time.time() - start)
def task(pid):
"""
Some non-deterministic task
@pydong
pydong / inheritance_no_super.py
Created December 23, 2013 00:30
python inheritance without super
class Person(object):
def __init__(self,fname='', lname=''):
self.fname = fname
self.lname = lname
def __repr__(self):
return 'repr: '+ self.fname + ' ' + self.lname
def __str__(self):
return 'str: ' + self.fname + ' ' + self.lname
class Person(object):
def __init__(self,fname='', lname=''):
self.fname = fname
self.lname = lname
def __repr__(self):
return 'repr: '+ self.fname + ' ' + self.lname
def __str__(self):
return 'str: ' + self.fname + ' ' + self.lname