Gevent network library Denis Bilenko gevent.org. Problem statement from urllib2 import urlopen response = urlopen('http://gevent.org') body = response.read()

Slides:



Advertisements
Similar presentations
Supporting Persistent Objects In Python Jeremy Hylton
Advertisements

Why killing tasklets is good practice
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Tornado Web and MongoDB An Introduction to AsyncMongo MongoBoston John C. Zablocki Development Manager, HealthcareSource Organizer, Beantown.
Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Intro to Boto (and gevent, ) Presented
NDB The new Python client library for the Google App Engine Datastore
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
N ODE.J S S ERVER S IDE J AVASCRIPT Diana Roiswati ( ) Ahmad Syafii ( ) Asri Taraqiadiyu ( )
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Fundamentals of Python: From First Programs Through Data Structures
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 6.
Node.js - What is Node.js? -
A Revolutionary Programming Pattern that Will Clean up your Code : Coroutines in C++ David Sackstein ACCU 2015.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Continuations And Java Regis -
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
Stackless Python: programming the way Guido prevented it intended.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
Dynamic Architectures (Component Reconfiguration) with Fractal.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Processes & Threads Introduction to Operating Systems: Module 5.
Threads A thread is an alternative model of program execution
LECTURE 11 Networking Part 2. NETWORKING IN PYTHON At this point, we know how to write a simple TCP client and simple TCP server using Python’s raw sockets.
MULTIPROCESSING MODULE OF PYTHON. CPYTHON  CPython is the default, most-widely used implementation of the Python programming language.  CPython - single-threaded.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
Multithreading vs. Event Driven in Code Development of High Performance Servers.
 Home Security System Willard Stanley. Motivations  Commercial security systems have a monthly cost  Designing my own allows for greater extensibility.
Chapter 4 – Thread Concepts
Introduction to threads
NodeJS and MEAN cs6320.
Lecture 11 Networking Part 2.
Chapter 4: Threads.
Node.Js Server Side Javascript
Operating Systems Review ENCE 360.
Topics Covered What is Real Time Operating System (RTOS)
CSE 775 – Distributed Objects Submitted by: Arpit Kothari
Processes and Threads Processes and their scheduling
EE2E1. JAVA Programming Revision Lecture.
Day 12 Threads.
NodeJS and MEAN Prof. L. Grewe.
Chapter 4 – Thread Concepts
CS399 New Beginnings Jonathan Walpole.
System Structure and Process Model
Node.Js Server Side Javascript
System Structure and Process Model
System Structure B. Ramamurthy.
System Structure and Process Model
Google App Engine Ying Zou 01/24/2016.
Lecture Topics: 11/1 General Operating System Concepts Processes
Process Description and Control
Threads and Concurrency
Не синхронный Python.
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Jonathan Walpole Computer Science Portland State University
Threads Chapter 5 2/23/2019 B.Ramamurthy.
Concurrency: Processes CSE 333 Summer 2018
CrawlBuddy The web’s best friend.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Presentation transcript:

gevent network library Denis Bilenko gevent.org

Problem statement from urllib2 import urlopen response = urlopen(' body = response.read() How to manage concurrent connections?

Problem statement def on_response_read(response): d = response.read() d.addCallbacks(on_body_read, on_error) def on_error(error):... def on_body_read(body):... d = readURL(' d.addCallbacks(on_response_read, on_error) reactor.run() Possible answer: Async framework (Twisted, asyncore,...)

simplicity is lost

Problem statement from threading import Thread def read_url(url): response = urllib2.urlopen(url) body = response.read() t1=Thread(target=read_url, args=(' t1.start() t2=Thread(target=read_url, args=(' t2.start() t1.join() t2.join() Possible answer: Threads

resource hog

Memory required for 10k connections

gevent (greenlet + libevent) from gevent import monkey; monkey.patch_all() def read_url(url): response = urllib2.urlopen(url) body = response.read() a = gevent.spawn(read_url, ' b = gevent.spawn(read_url, ' gevent.joinall([a, b])

concurrent fetch

Memory required for 10k connections

greenlet

from greenlet import greenlet >>> def myfunction(arg):... return arg + 1 >>> g = greenlet(myfunction) >>> g.switch(2) 3

from greenlet import greenlet >>> MAIN = greenlet.getcurrent() >>> def myfunction(arg):... MAIN.switch('hello')... return arg + 1 >>> g = greenlet(myfunction) >>> g.switch(2) 'hello' >>> g.switch('hello to you') 3

switching deep down the stack >>> def myfunction(arg):... MAIN.switch('hello')... return arg + 1 >>> def top_function(arg):... return myfunction(arg) >>> g = greenlet(top_function) >>> g.switch(2) 'hello'

from greenlet import greenlet primitive pseudothreads, share same OS thread switched explicitly via switch() and throw() organized in a tree, each has.parent except MAIN switch(), throw() and.parent reserved for gevent

How gevent uses greenlet HUB MAIN spawned greenlets

Hub: greenlet that runs event loop from gevent import core class Hub(greenlet.greenlet): def run(self): core.dispatch() # wrapper for event_dispatch() def get_hub(): # return the global Hub instance # creating one if does not exist gevent/hub.py

Event loop libevent 1.4.x or beta gevent.core: wraps libevent API (like pyevent) >>> def print_hello():... print 'hello' >>> gevent.core.timer(1, print_hello) >>> gevent.core.dispatch() hello 1 # return value (no more events)

Implementation of gevent.sleep() def sleep(seconds=0): """Put the current greenlet to sleep""“ switch = getcurrent().switch timer = core.timer(seconds, switch) try: get_hub().switch() finally: timer.cancel()

Cooperative socket gevent.socket: compatible synchronous interface wraps a non-blocking socket def recv(self, size): while True: try: return self._sock.recv(size) except error, ex: if ex[0] == EWOULDBLOCK: wait_read(self.fileno()) else: raise

Cooperative socket gevent.socket: compatible synchronous interface wraps a non-blocking socket def wait_read(fileno): switch = getcurrent().switch event = core.read_event(fileno, switch) try: get_hub().switch() finally: event.cancel() gevent/socket.py

Cooperative socket gevent.socket dns queries are resolved through libevent-dns (getaddrinfo, gethostbyname) gevent.ssl

Monkey patching from gevent import monkey; monkey.patch_all() def read_url(url): response = urllib2.urlopen(url) body = response.read() a = gevent.spawn(read_url, ' b = gevent.spawn(read_url, ' gevent.joinall([a, b])

Monkey patching Patches: socket and ssl modules time.sleep, select.select thread and threading Beware: libraries that wrap C libraries (e.g. MySQLdb) Disk I/O things not yet patched: subprocess, os.system, sys.stdin Tested with httplib, urllib2, mechanize, mysql-connector, SQLAlchemy,...

Greenlet objects from gevent import monkey; monkey.patch_all() def read_url(url): response = urllib2.urlopen(url) body = response.read() a = gevent.spawn(read_url, ' b = gevent.spawn(read_url, ' gevent.joinall([a, b])

Greenlet objects def read_url(url): response = urllib2.urlopen(url) body = response.read() g = Greenlet(read_url, url) g.start() # wait for it to complete g.join() # or raise an exception and wait to exit g.kill() = spawn

Greenlet objects def read_url(url): response = urllib2.urlopen(url) body = response.read() g = Greenlet(read_url, url) g.start() # wait for it to complete (or timeout expires) g.join(timeout=2) # or raise and wait to exit (or timeout expires) g.kill(timeout=2) = spawn

Timeouts with gevent.Timeout(5): response = urllib2.urlopen(url) for line in response: print line # raises Timeout if not done after 5 seconds with gevent.Timeout(5, False): response = urllib2.urlopen(url) for line in response: print line # exits block if not done after 5 seconds Beware: catch-all “ except: ”, non-yielding code

API socket, ssl Greenlet Timeout Event, AsyncResult Queue (also JoinableQueue, PriorityQueue, LifoQueue) – Queue(0) is a synchronous channel Pool StreamServer: TCP and SSL servers WSGI servers

gevent.wsgi – uses libevent-http – efficient, but lacks important features gevent.pywsgi – uses gevent sockets green unicorn (gunicorn.org) – its own parser or gevent’s server – pre-fork workers

Caveat emptor Reduced portability – no Jython, IronPython – not all platforms supported by CPython PyThreadState is shared – exc_info (saved/restored by gevent) – tracing, profiling info

Future plans alternative coroutine libraries – Stackless – swapcontext more libevent: – http client – buffered socket operations – priorities process handling (gevent.subprocess) even more stable API with 1.0

Examples bitbucket.org/denis/gevent/src/tip/examples/ chat.gevent.org omegle.com ProjectsUsingGevent – gevent-mysql – psycopg2 bit.ly/use-gevent – websockets, web crawlers, facebook apps

Summary coroutines are easy-to-use threads as efficient as async libraries works well if app is I/O bound simple API, many things familiar works with unsuspecting 3 rd party modules

Thank you!