understanding non blocking i/o with python
TRANSCRIPT
![Page 1: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/1.jpg)
Understanding Non-Blocking I/O
Vaidik Kapoorgithub.com/vaidik
EuroPython 2015
![Page 2: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/2.jpg)
High Level Overview
● What is Non Blocking I/O?● Understanding by examples● Why should you care?● Disclaimer: a rather beginner level
introduction to the topic
![Page 3: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/3.jpg)
Who am I?
1. Pythonista for about 4 years2. Infrastructure Engineer at Wingify (responsible
for all things systems and operations)3. Based out of New Delhi, India4. Social networks:
a. github.com/vaidikb. twitter.com/vaidikkapoor
![Page 4: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/4.jpg)
Some Background
1. Started out as a web developer and moved down the stack
2. Encountered Gevent along the journey3. Always wondered - how does this thing
really work4. Nobody talks about it
![Page 5: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/5.jpg)
Non-Blocking I/O
OR
What is blocking?
![Page 6: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/6.jpg)
What is Blocking?
A function or a code-block is blocking if ithas to wait for anything to complete.
![Page 7: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/7.jpg)
Blocking
1. A blocking function is capable of delaying execution of other tasks, especially those that are independenta. In case of a server, other requests may get blockedb. In case of a worker consuming tasks from a queue,
other independent tasks may get delayed2. The overall system is not able to progress
![Page 8: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/8.jpg)
I/O
At least for today’s applications (not exhaustive):1. Dealing with the network2. Reading from or writing to disk3. Operations on Pipe4. Basically, any kind of operation on a file
descriptor (in *NIX terminology).
![Page 9: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/9.jpg)
Non-Blocking I/O
Dealing with I/O in a way so that executiondoes not get delayed because of it.
![Page 10: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/10.jpg)
Server / Client
![Page 11: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/11.jpg)
Server / Client
![Page 12: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/12.jpg)
Server / Client
![Page 13: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/13.jpg)
Server / Client
![Page 14: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/14.jpg)
Server / Client
![Page 15: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/15.jpg)
$ time python example1-client.py
python example1.1-client.py 0.05s user 0.08s system 0% cpu 45.050 total
![Page 16: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/16.jpg)
Non-Blocking Network I/O in Python
At the most basic level, it’s all about:
$ pydoc socket.socket.setblocking
socket.socket.setblocking = setblocking(...) unbound socket._socketobject method
setblocking(flag)
Set the socket to blocking (flag is true) or non-blocking (false).
setblocking(True) is equivalent to settimeout(None);
setblocking(False) is equivalent to settimeout(0.0).
![Page 17: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/17.jpg)
Server / Client v2
![Page 18: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/18.jpg)
$ time python example2-client.py
Traceback (most recent call last):
File "example2-client.py", line 9, in <module>
assert sent == len(data), '%s != %s' % (sent, len(data))
AssertionError: 457816 != 73400320
python example2-client.py 0.06s user 0.06s system 89% cpu 0.136 total
![Page 19: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/19.jpg)
Server / Client v3
![Page 20: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/20.jpg)
Server / Client v4
![Page 21: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/21.jpg)
Understanding select()
● A system call for monitoring events on file descriptors
● select.select() just wraps the select syscall○ It does make things much simpler than C○ If you can understand this, then working with the C
API would be much simpler
![Page 22: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/22.jpg)
select.select = select(...)
select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)
Understanding select()
● Takes three sets of fds for monitoring them for reading, writing and exceptions
● Returns three sets with fds that are ready to be read from, written to or handled for exception
![Page 23: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/23.jpg)
Client v5
![Page 24: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/24.jpg)
select and family
1. Other implementations for monitoring file descriptors:a. poll - Unix/Linuxb. epoll - Linuxc. kqueue - BSD
2. The de-facto today - epoll and kqueue.
![Page 25: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/25.jpg)
One library to rule them all
1. libevent2. libev3. libuv4. more?
![Page 26: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/26.jpg)
1. Geventa. Greenlet basedb. C extensionc. Probably the easiest to start with for all practical purposes
2. Eventleta. Greenlet basedb. Pure Python
In Python World (Libraries)
![Page 27: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/27.jpg)
1. Twisteda. Mainloop is called Reactorb. Almost all commonly used protocols implemented c. Pure Pythond. Not very-well suited for web apps
2. Tornadoa. Mainloop is called IOLoopb. Pure Pythonc. More focussed for writing webapps
In Python World (Frameworks)
![Page 28: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/28.jpg)
1. asyncio
In Python World (Frameworks)
![Page 29: Understanding Non Blocking I/O with Python](https://reader030.vdocuments.net/reader030/viewer/2022032700/55d36a84bb61eb5c428b468f/html5/thumbnails/29.jpg)
Questions?