Skip to main content

Several gevent utilities

Project description

Utilities for gevent.

There’re several gevent.Greenlet subclasses:

  • Processlet – maximizing multi-core use in gevent environment.

  • Transparentlet – keeping exc_info rather than printing exception.

And etc.:

  • ObjectPool – pooling objects. (e.g. connection pool)

See the next examples.

Examples

Processlet for bcrypt

bcrypt is a library to hash password. That the hashing is very heavy CPU-bound task. You can’t guarantee concurrency with only gevent. Use Processlet instead:

import bcrypt
import gevent
from lets import Processlet

# bcrypt.hashpw is very heavy cpu-bound task.  it can spend a few seconds.
def hash_password(password, salt=bcrypt.gensalt()):
    return bcrypt.hashpw(str(password), salt)

def tictoc(delay=0.1):
    while True:
        print '.'
        gevent.sleep(delay)

passwords = ['alfa', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot',
             'golf', 'hotel', 'india', 'juliett', 'kilo', 'lima', 'mike',
             'november', 'oscar', 'papa', 'quebec', 'romeo', 'sierra',
             'tango', 'uniform', 'victor', 'whiskey', 'xray', 'yankee',
             'zulu']

# start tictoc
gevent.spawn(tictoc)

# Greenlet, tictoc pauses for a few seconds
greenlet = gevent.spawn(hash_password, passwords[0])
password_hash_0 = greenlet.get()

# Processlet, tictoc never pauses
processlet_1 = Processlet.spawn(hash_password, passwords[1])
processlet_2 = Processlet.spawn(hash_password, passwords[2])
password_hash_1 = processlet_1.get()
password_hash_2 = processlet_2.get()

You can also limit the number of child processes with ProcessPool:

import multiprocessing
from lets import ProcessPool

pool_size = max(multiprocessing.cpu_count() - 1, 1)
pool = ProcessPool(pool_size)
password_hashes = pool.map(hash_password, passwords)

Memcached connection pool

Greenlet-safe connection pool can be easily implemented by ObjectPool:

import memcache
from lets import ObjectPool

mc_pool = ObjectPool(10, memcache.Client, [('localhost', 11211)])

def save(key, val):
    with mc_pool.reserve() as mc:
        mc.set(key, val)

for x, password_hash in enumerate(password_hashes):
    gevent.spawn(save, 'password_hashes[%d]' % x, password_hash)

gevent.wait()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lets-0.0.16.tar.gz (8.7 kB view details)

Uploaded Source

File details

Details for the file lets-0.0.16.tar.gz.

File metadata

  • Download URL: lets-0.0.16.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lets-0.0.16.tar.gz
Algorithm Hash digest
SHA256 bbfa1b12af411d932747a048fd9878f39380c55aadd2f4ea510c41f5d1bee9f4
MD5 61f234566a6a1a29de1a8b905e2fafb3
BLAKE2b-256 2a0d2b80671ab45fc4ad6dda962772505d77cd2fe9e64c700c29796851e4d206

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page