14
Super Fast Gevent By Walter Liu

Super Fast Gevent Introduction

Embed Size (px)

Citation preview

Page 1: Super Fast Gevent Introduction

Super Fast GeventBy Walter Liu

Page 2: Super Fast Gevent Introduction

Agenda• Simple Example• About Gevent• About libevent• Monkey patch of gevent• Other notes• About monkey patch

Page 3: Super Fast Gevent Introduction

C10K problem and Gevent Performance

Page 4: Super Fast Gevent Introduction

Simple Example

Page 5: Super Fast Gevent Introduction
Page 6: Super Fast Gevent Introduction
Page 7: Super Fast Gevent Introduction

About GEvent• Fast event loop based on libevent or libevent2• Light weight execution unit based on greenlets• Use Monkey patch• See monkey patch to know what it works on.

• Simple API• Fast WSGI server

Page 8: Super Fast Gevent Introduction

libevent• /dev/poll• kqueue(2)• event ports• POSIX select(2)• Windows select()• poll(2)• epoll(4)

• Diff in different platform

Page 9: Super Fast Gevent Introduction

Gevent Monkey Patch• socket (dns=True)• ssl• os.fork()• time.sleep()• select• threading => become co-routine• _thread_local.local

• subprocess• sys• stdin, stdout, stderr

Page 10: Super Fast Gevent Introduction

Other Notes• Monkey patch• It’s patching sys.modules, so it’s global patching.• Apply monkey patch before other import. (especially on thread)• Apply monkey patch after fork/multi-process.• Be noted about 3rd party library compatibility

• Be noted about blocking functions in your program.• Gevent with Python web framework• Played uWSGI with gevent. (very good performance)• Tried using it in Django to accelerate crawling.• How about gunicorn?

Page 11: Super Fast Gevent Introduction

Monkey patch

Page 12: Super Fast Gevent Introduction

Monkey patch example

Page 13: Super Fast Gevent Introduction

Good for unit test

Page 14: Super Fast Gevent Introduction

Tips• Life time of monkey patch is till the end of process.• You may chose not to monkey unpatch, but it may breaks the

whole program.• Basically, it’s a dirty workaround.• Thus, only use it on unit test or something like gevent.