async web and python

17
Jasim Muhammed ASYNC WEB AND PYTHON

Upload: jasim-muhammed

Post on 12-Apr-2017

212 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Async Web and Python

Jasim Muhammed

ASYNC WEB AND PYTHON

Page 2: Async Web and Python

ME?• Senior Software Architect,

Fetchr, Dubai• Hacking around PostgreSQL,

NoSQL, Python, Go, C

Page 3: Async Web and Python

SINGLE DELIVERY GUY SHOP

Pic source: https://www.nginx.com/blog/thread-pools-boost-performance-9x/

Page 4: Async Web and Python

ASYNC DELIVERY AT SHOP

Pic source: https://www.nginx.com/blog/thread-pools-boost-performance-9x/

Page 5: Async Web and Python

HOW REQUEST IS PROCESSED?

Browser

Page 6: Async Web and Python

APACHE REQUEST MODES

• Prefork

• Worker

• Event

Page 7: Async Web and Python

PYTHON CONCURRENCY OPTIONS

• Multiprocessing

• Threading

• Async processes

Page 8: Async Web and Python

PYTHON CONCURRENCY OPTIONSTheory Reality

Source: https://www.reddit.com/r/aww/comments/2oagj8/multithreaded_programming_theory_and_practice

Page 9: Async Web and Python

ISSUES WITH MULTIPROCESSING

• Time to create process

• High processor usage

• Private memory space for processes

Page 10: Async Web and Python

ISSUES WITH THREADING

• Python threads are run in serial due to GIL

• High processor usage

Page 11: Async Web and Python

ASYNC ADVANTAGES

• Process request when an IO bound operation happens

• No high processor usage

• Moderate operations

Page 12: Async Web and Python

ASYNC LIBRARIES

• Gevent (Python 2.7)

• Asyncio(Python 3)

Page 13: Async Web and Python

DISTRIBUTED JOB SERVERCELERY

• Offload heavy processing

• Distribute processing to multiple servers

• Cron or maintenance jobs

• Easier to implement Webhooks

Page 14: Async Web and Python

HOW CELERY HELPS INCONCURRENT REQUESTS?

• Request processing is done very fast

• Offloads heavy computation or third party request services to job server workers.

• Releases the application server workers for further requests

Page 15: Async Web and Python

CELERY TASK EXAMPLE• Regular Async works without any returns

• Mail sending

• SMS Sending

• In case Task computation result is important

• Report generation

• Cron/Maintenance jobs like temporary files cleanup

Page 16: Async Web and Python

BEST PRACTISES• Separate queue for separate tasks

• Run workers to multiple nodes

• Task retry is there, but be careful. In the case of task failed, you can push to new queue

• Don’t restart workers with ‘KILL -9’, use ‘KILL -15’ or SIGTERM

• Pass only json serialisable data from to celery.

• Use RabbitMQ for broker and Redis for result backend

• AsyncDelay and JobID