Code for Python Concurrency Getting Started Course on Pluralsight.
GIL_less Python interpreters
- Jython
- IronPython
Python Multiprocessing
- Sidesteps GIL
- Less need for synchronization
- Can be paused and terminate
- More resilient
- Higher memory footprint
- Expensive context switches
is the process whereby a Python object hierarchy is converted into a byte stream, 'unpacking' is the inverse operation
- None, True, False
- integers, floats, complex numbers
- Normal and Unicode strings
- Collections containing only picklable objects
- Top level functions
- Classes with picklable attributes
is a child process that does not prevent its parent process from exiting
- is_alive()
- terminate()
- Shared resources may be put in an inconsistent state
- Finally clauses and exit handlers will not be run
''' class multiprocessing.Pool([num_processes # default is num of cpu cors [,initializer [,initargs # picklable not required [maxtasksperchild]]] ]) ''''
''' map(func, iterable[,chunksize]) '''
- Pipe
- Queue
| threading.Queue | multiprocessing.Queue | multiprocessing.JoinableQueue |
|---|---|---|
| qsize() | qsize | qsize |
| put() | put() | put() |
| get() | get() | get() |
| empty() | empty() | empty() |
| full() | full() | full() |
| task_done() | -- task_done() -- | task_done() |
| join() | join() | join() |
- Shared Memory
- multiprocessing.Value
- Manager Process
- multiprocessing.Array
'''python multiprocessing.Value(typecode_or_type, *args[, lock]) '''
| ctypes type | Ctype | Python type | Type-code |
|---|---|---|---|
| c_bool | _Bool | bool(1) | |
| c_char | char | 1-character bytes object | 'c' |
| c_wchar | wchar_t | 1-character string object | 'u' |
| c_int | int | int | 'i' |
| c_long | long | int | 'l' |
| c_float | float | float | 'f' |
| c_char_p | char *(NUL terminated | bytes object or None | |
| c_wchar_p | wchar_t * (NUL terminated | string object or None | |
| c_void_p | void * | int or None | - |
| Value | Array |
|---|---|
| Dictionary | List |
| Lock | Semaphore |
| Data Structures | Synchronization Mechanisms |
|---|---|
| Value | Lock |
| Array | RLock |
| List | BoundedSemaphore |
| Dict | Event |
| Namespace | Condition |
| Queue | Barrier |
- Tasks suspend themselves to allow others run
- Event loop resumes the task when the IO operation completes
- Tasks => Coroutines
- Coroutine Function
- Coroutine Object
pip install aiohttp
- aiomysql
- aiopg
- aiocouchdb
- aiocassandra