Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

"""Provide gevent compatibility. 

 

Usage: 

>>> from handel.gevent import patch 

>>> patch() 

""" 

 

from __future__ import absolute_import 

 

from functools import wraps 

from gevent import threadpool 

 

from . import interface 

 

 

# Green pool 

 

# The 'maxsize=1' argument provides implicit locking 

POOL = threadpool.ThreadPool(maxsize=1) 

 

 

# Green decorator 

 

def green(func, pool=POOL): 

"""Make a given function gevent-compatible by running 

it in a gevent threadpool.""" 

 

@wraps(func) 

def wrapper(*args, **kwargs): 

return pool.apply(func, args, kwargs) 

 

return wrapper 

 

 

# Patch function 

 

def patch(): 

"""Provide gevent compatibility. 

 

Usage: 

>>> from handel.gevent import patch 

>>> patch() 

""" 

# Gevent-compatible version of handel FFI library 

gevent_handel = type('GeventFFILibrary', (), {})() 

# Populate gevent_handel 

for name in dir(interface.handel): 

if name.startswith('xia'): 

func = getattr(interface.handel, name) 

setattr(gevent_handel, name, green(func)) 

# Patch 

interface.handel = gevent_handel