├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST.in ├── Makefile ├── README.md ├── atomos ├── __about__.py ├── __init__.py ├── atom.py ├── atomic.py ├── multiprocessing │ ├── __init__.py │ ├── atom.py │ └── atomic.py └── util.py ├── docs ├── .DS_Store ├── Makefile ├── _themes │ ├── LICENSE │ ├── README │ ├── flask │ │ ├── layout.html │ │ ├── relations.html │ │ ├── static │ │ │ ├── flasky.css_t │ │ │ └── small_flask.css │ │ └── theme.conf │ ├── flask_small │ │ ├── layout.html │ │ ├── static │ │ │ └── flasky.css_t │ │ └── theme.conf │ └── flask_theme_support.py ├── conf.py └── index.rst ├── setup.py ├── tests ├── test_aref.py ├── test_atom.py ├── test_atomic.py └── test_util.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | # Backup files 2 | *.~ 3 | 4 | # Byte-compiled / optimized / DLL files 5 | __pycache__/ 6 | *.py[cod] 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | bin/ 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | .eggs 26 | MANIFEST 27 | .pydev 28 | .pydevproject 29 | *.eclipse* 30 | .project 31 | # Installer logs 32 | pip-log.txt 33 | pip-delete-this-directory.txt 34 | 35 | # Unit test / coverage reports 36 | .tox/ 37 | .coverage* 38 | .cache 39 | nosetests.xml 40 | coverage.xml 41 | 42 | # Translations 43 | *.mo 44 | 45 | # Sphinx documentation 46 | docs/_build/ 47 | 48 | # vim 49 | *.swp 50 | *.swo 51 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - 2.7 4 | - 3.3 5 | - 3.4 6 | - 3.5 7 | - 3.6 8 | script: python setup.py test 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Max Countryman 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.md 2 | include LICENSE 3 | exclude Makefile 4 | exclude tox.ini 5 | prune docs 6 | prune tests 7 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | clean: 2 | @find . -iname '*.pyc' -delete 3 | @find . -iname '*.pyo' -delete 4 | @find . -iname '*~' -delete 5 | @find . -iname '*.swp' -delete 6 | @find . -iname '__pycache__' -delete 7 | @rm -rf dist/ 8 | 9 | cleaneggs: 10 | @find . -name '*.egg' -print0|xargs -0 rm -rf -- 11 | @rm -rf .eggs/ 12 | @rm -rf *.egg-info/ 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Atomos 2 | **Atomic primitives for Python.** 3 | 4 | [![Build Status](https://travis-ci.org/maxcountryman/atomos.svg?branch=master)](https://travis-ci.org/maxcountryman/atomos) 5 | 6 | Atomos is a library of atomic primitives, inspired by Java's 7 | java.util.concurrent.atomic. It provides atomic types for bools, ints, longs, 8 | and floats as well as a generalized object wrapper. In addition, it introduces 9 | [atoms](http://clojure.org/atoms), a concept Clojure programmers will be 10 | familiar with. 11 | 12 | ## Motivation 13 | Mutable shared state is hard and guess what, it's ubuiquitous in Python. When 14 | working in a multi-threaded context or whenever an application is racing, locks 15 | can be a useful tool. However they can quickly become unweildy. 16 | 17 | To address this, Atomos provides wrappers around primitives and objects which 18 | handle the locking semantics for us. These special primitives allow for writing 19 | cleaner, simpler code without having to orchestrate locks directly. 20 | 21 | In particular Atomos introduces atoms, a new data type for managing shared 22 | mutable state. Atoms are a near-direct port of Clojure's eponymous data type. 23 | They work by wrapping a given object in compare-and-set semantics. 24 | 25 | ## Installation 26 | 27 | Atomos is available via PyPI. 28 | 29 | ```shell 30 | $ pip install atomos 31 | ``` 32 | 33 | ## Usage 34 | Say we have some shared state in our application. Maybe we have a chat 35 | server which holds state in memory about connected clients. If our 36 | application is threaded we will need some way of sharing this state between 37 | threads. 38 | 39 | We can model this state as an atom. This will ensure that when multiple threads 40 | update and retrieve the state, its value is always consistent. For example: 41 | 42 | ```python 43 | >>> import atomos.atom as atom 44 | >>> state = atom.Atom({'conns': 0, 'active_clients': set([])}) 45 | ``` 46 | 47 | Our `state` is an `Atom`, which means we can update it using its `swap` method. 48 | This method works by taking a function which will take the existing state of 49 | the atom and and any arguments or keyword arguments we provide it. It should 50 | return an updated state. 51 | 52 | For instance, as a client connects, we want to update the number of connections 53 | and the active client set. We can write a function which we can then pass to 54 | swap to safely mututate our state: 55 | 56 | ```python 57 | >>> def new_client(cur_state, client): 58 | ... cur_state['conns'] += 1 59 | ... cur_state['active_clients'].add(client) 60 | ... return cur_state 61 | >>> state.swap(new_client, 'foo') 62 | ``` 63 | 64 | Here we have updated our state and can be sure that any other thread which may 65 | have looked at the state only ever saw the state as it was before we called 66 | `swap` or after. However any race condition which might have existed between 67 | incrementing the connections count and adding the client is eliminated, thanks 68 | to our use of the atom. 69 | 70 | ### Atomic Primitives 71 | Atomos also provides atomic primitives as wrappers around `int`, `long`, 72 | `float`, and `bool` as well as a general wrapper around any object type. We can 73 | use these primitives to construct a thread-safe counter: 74 | 75 | ```python 76 | >>> import atomos.atomic 77 | >>> counter = atomos.atomic.AtomicInteger() 78 | >>> counter.get() 79 | 0 80 | ``` 81 | 82 | To increment the counter, we can call `counter.add_and_get(1)`. This will 83 | return the new value back to us, `1`. 84 | 85 | For more complex object types we can use an `AtomicReference`. For instance, we 86 | can wrap any arbitrary class and protect updates to its value like this: 87 | 88 | ```python 89 | >>> class MyState(object): 90 | ... def __init__(self, foo, bar): 91 | ... self.foo = foo 92 | ... self.bar = bar 93 | >>> state = atomos.atomic.AtomicReference(MyState(42, False)) 94 | ``` 95 | 96 | So long as we interact with the `MyState` instance via the `state` wrapper, our 97 | updates will always be protected. 98 | 99 | ## Multiprocessing 100 | Now it works with [multiprocessing](https://docs.python.org/3.4/library/multiprocessing.html). 101 | 102 | Just use the following import path: 103 | 104 | ```python 105 | import atomos.multiprocessing.atomic 106 | ``` 107 | 108 | ## Contribution 109 | Contributions are welcome, please ensure PEP8 is followed and that new code is 110 | well-tested prior to making a pull request. 111 | -------------------------------------------------------------------------------- /atomos/__about__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __title__ = 'atomos' 3 | __version_info__ = ('0', '3', '1') 4 | __version__ = '.'.join(__version_info__) 5 | __author__ = 'Max Countryman' 6 | __license__ = 'BSD' 7 | __copyright__ = 'Copyright 2014 Max Countryman' 8 | -------------------------------------------------------------------------------- /atomos/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | Atomic primitives for Python. 4 | ''' 5 | -------------------------------------------------------------------------------- /atomos/atom.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | atomos.atom 4 | 5 | Atom data type. 6 | ''' 7 | 8 | import collections 9 | 10 | import atomos.atomic as atomic 11 | import atomos.util as util 12 | 13 | 14 | class ARef(object): 15 | ''' 16 | Ref object super type. 17 | 18 | Refs may hold watches which can be notified when a value a ref holds 19 | changes. In effect, a watch is a callback which receives the key, 20 | object reference, oldval, and newval. 21 | 22 | For example, a watch function could be constructed like this:: 23 | 24 | >>> def watch(k, ref, old, new): 25 | ... print k, ref, old, new 26 | >>> aref = ARef() 27 | >>> aref.add_watch(watch) 28 | 29 | However note that `ARef` should generally be subclassed, a la `Atom`, as it 30 | does not independently hold any value and functions merely as a container 31 | for the watch semantics. 32 | ''' 33 | def __init__(self): 34 | self._watches = {} 35 | 36 | def get_watches(self): 37 | ''' 38 | Returns the watches dictionary. 39 | ''' 40 | return self._watches 41 | 42 | @util.synchronized 43 | def add_watch(self, key, fn): 44 | ''' 45 | Adds `key` to the watches dictionary with the value `fn`. 46 | 47 | :param key: The key for this watch. 48 | :param fn: The value for this watch, should be a function. Note that 49 | this function will be passed values which should not be mutated 50 | wihtout copying as other watches may in turn be passed the same 51 | eference! 52 | ''' 53 | self._watches[key] = fn 54 | 55 | @util.synchronized 56 | def remove_watch(self, key): 57 | ''' 58 | Removes `key` from the watches dictionary. 59 | 60 | :param key: The key of the watch to remove. 61 | ''' 62 | self._watches.pop(key, None) 63 | 64 | def notify_watches(self, oldval, newval): 65 | ''' 66 | Passes `oldval` and `newval` to each `fn` in the watches dictionary, 67 | passing along its respective key and the reference to this object. 68 | 69 | :param oldval: The old value which will be passed to the watch. 70 | :param newval: The new value which will be passed to the watch. 71 | ''' 72 | watches = self._watches.copy() 73 | for k in watches: 74 | fn = watches[k] 75 | if isinstance(fn, collections.Callable): 76 | fn(k, self, oldval, newval) 77 | 78 | 79 | class Atom(ARef): 80 | ''' 81 | Atom object type. 82 | 83 | Atoms store mutable state and provide thread-safe methods for retrieving 84 | and altering it. This is useful in multi-threaded contexts or any time an 85 | application makes use of shared mutable state. By using an atom, it is 86 | possible to ensure that read values are always consistent and that write 87 | values do not yield unexpected state (e.g. data loss). 88 | 89 | For example, if an application uses a dictionary to store state, using an 90 | atom will guarantee that the dictionary is never in an inconsistent state 91 | as it is being updated:: 92 | 93 | >>> state = Atom({'active_conns': 0, 'clients': set([])}) 94 | >>> def new_client(cur_state, client): 95 | ... new_state = cur_state.copy() 96 | ... new_state['clients'].add(client) 97 | ... new_state['active_conns'] += 1 98 | ... return new_state 99 | >>> state.swap(new_client, 'foo') 100 | 101 | In the above example we use an atom to store state about connections. Our 102 | mutation function, `new_client` is a function which takes the existing 103 | state contained by the atom and a new client. Any part of our program which 104 | reads the atom's state by using `deref` will always see a consistent view 105 | of its value. 106 | 107 | This is particularly useful when altering shared mutable state which cannot 108 | be changed atomically. Atoms enable atomic semantics for such objects. 109 | 110 | Because atoms are themselves refs and inherit from `ARef`, it is also 111 | possible to add watches to them. Watches can be thought of as callbacks 112 | which are invoked when the atom's state changes. 113 | 114 | For example, if we would like to log each time a client connects, we can 115 | write a watch that will be responsible for this and then add it to the 116 | state atom:: 117 | 118 | >>> state = Atom({'active_conns': 0, 'clients': set([])}) 119 | >>> def log_new_clients(k, ref, old, new): 120 | ... if not new['active_conns'] > old['active_conns']: 121 | ... return 122 | ... old_clients = old['clients'] 123 | ... new_clients = new['clients'] 124 | ... print 'new client', new_clients.difference(old_clients) 125 | >>> state.add_watch('log_new_clients', log_new_clients) 126 | 127 | We have added a watch which will print out a message when the client count 128 | has increased, i.e. a client has been added. Note that for a real world 129 | application, a proper logging facility should be preferred over print. 130 | 131 | Watches are keyed by the first value passed to `add_watch` and are invoked 132 | whenever the atom changes with the key, reference, old state, and new state 133 | as parameters. 134 | 135 | Note that watch functions may be called from multiple threads at once and 136 | therefore their ordering is not guaranteed. For instance, an atom's state 137 | may change, and before the watches can be notified another thread may alter 138 | the atom and trigger notifications. It is possible for the second thread's 139 | notifications to arrive before the first's. 140 | ''' 141 | def __init__(self, state): 142 | super(Atom, self).__init__() 143 | self._state = atomic.AtomicReference(state) 144 | 145 | def __repr__(self): 146 | return util.repr(__name__, self, self._state._value) 147 | 148 | def deref(self): 149 | ''' 150 | Returns the value held. 151 | ''' 152 | return self._state.get() 153 | 154 | def swap(self, fn, *args, **kwargs): 155 | ''' 156 | Given a mutator `fn`, calls `fn` with the atom's current state, `args`, 157 | and `kwargs`. The return value of this invocation becomes the new value 158 | of the atom. Returns the new value. 159 | 160 | :param fn: A function which will be passed the current state. Should 161 | return a new state. This absolutely *MUST NOT* mutate the 162 | reference to the current state! If it does, this function may loop 163 | indefinitely. 164 | :param \*args: Arguments to be passed to `fn`. 165 | :param \*\*kwargs: Keyword arguments to be passed to `fn`. 166 | ''' 167 | while True: 168 | oldval = self.deref() 169 | newval = fn(oldval, *args, **kwargs) 170 | if self._state.compare_and_set(oldval, newval): 171 | self.notify_watches(oldval, newval) 172 | return newval 173 | 174 | def reset(self, newval): 175 | ''' 176 | Resets the atom's value to `newval`, returning `newval`. 177 | 178 | :param newval: The new value to set. 179 | ''' 180 | oldval = self._state.get() 181 | self._state.set(newval) 182 | self.notify_watches(oldval, newval) 183 | return newval 184 | 185 | def compare_and_set(self, oldval, newval): 186 | ''' 187 | Given `oldval` and `newval`, sets the atom's value to `newval` if and 188 | only if `oldval` is the atom's current value. Returns `True` upon 189 | success, otherwise `False`. 190 | 191 | :param oldval: The old expected value. 192 | :param newval: The new value which will be set if and only if `oldval` 193 | equals the current value. 194 | ''' 195 | ret = self._state.compare_and_set(oldval, newval) 196 | if ret: 197 | self.notify_watches(oldval, newval) 198 | 199 | return ret 200 | -------------------------------------------------------------------------------- /atomos/atomic.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | atomos.atomic 4 | 5 | Atomic primitives. 6 | ''' 7 | 8 | 9 | import six 10 | 11 | import atomos.util as util 12 | 13 | if six.PY3: 14 | long = int 15 | 16 | 17 | class AtomicReference(object): 18 | ''' 19 | A reference to an object which allows atomic manipulation semantics. 20 | 21 | AtomicReferences are particularlly useful when an object cannot otherwise 22 | be manipulated atomically. 23 | ''' 24 | def __init__(self, value=None): 25 | self._value = value 26 | self._lock = util.ReadersWriterLock() 27 | 28 | def __repr__(self): 29 | return util.repr(__name__, self, self._value) 30 | 31 | def get(self): 32 | ''' 33 | Returns the value. 34 | ''' 35 | with self._lock.shared: 36 | return self._value 37 | 38 | def set(self, value): 39 | ''' 40 | Atomically sets the value to `value`. 41 | 42 | :param value: The value to set. 43 | ''' 44 | with self._lock.exclusive: 45 | self._value = value 46 | return value 47 | 48 | def get_and_set(self, value): 49 | ''' 50 | Atomically sets the value to `value` and returns the old value. 51 | 52 | :param value: The value to set. 53 | ''' 54 | with self._lock.exclusive: 55 | oldval = self._value 56 | self._value = value 57 | return oldval 58 | 59 | def compare_and_set(self, expect, update): 60 | ''' 61 | Atomically sets the value to `update` if the current value is equal to 62 | `expect`. 63 | 64 | :param expect: The expected current value. 65 | :param update: The value to set if and only if `expect` equals the 66 | current value. 67 | ''' 68 | with self._lock.exclusive: 69 | if self._value == expect: 70 | self._value = update 71 | return True 72 | 73 | return False 74 | 75 | 76 | class AtomicBoolean(AtomicReference): 77 | ''' 78 | A boolean value whichs allows atomic manipulation semantics. 79 | ''' 80 | def __init__(self, value=False): 81 | super(AtomicBoolean, self).__init__(value=value) 82 | 83 | # We do not need a locked get since a boolean is not a complex data type. 84 | def get(self): 85 | ''' 86 | Returns the value. 87 | ''' 88 | return self._value 89 | 90 | def __setattr__(self, name, value): 91 | # Ensure the `value` attribute is always a bool. 92 | if name == '_value' and not isinstance(value, bool): 93 | raise TypeError('_value must be of type bool') 94 | 95 | super(AtomicBoolean, self).__setattr__(name, value) 96 | 97 | 98 | class AtomicNumber(AtomicReference): 99 | ''' 100 | AtomicNumber object super type. 101 | 102 | Contains common methods for AtomicInteger, AtomicLong, and AtomicFloat. 103 | ''' 104 | # We do not need a locked get since numbers are not complex data types. 105 | def get(self): 106 | ''' 107 | Returns the value. 108 | ''' 109 | return self._value 110 | 111 | def add_and_get(self, delta): 112 | ''' 113 | Atomically adds `delta` to the current value. 114 | 115 | :param delta: The delta to add. 116 | ''' 117 | with self._lock.exclusive: 118 | self._value += delta 119 | return self._value 120 | 121 | def get_and_add(self, delta): 122 | ''' 123 | Atomically adds `delta` to the current value and returns the old value. 124 | 125 | :param delta: The delta to add. 126 | ''' 127 | with self._lock.exclusive: 128 | oldval = self._value 129 | self._value += delta 130 | return oldval 131 | 132 | def subtract_and_get(self, delta): 133 | ''' 134 | Atomically subtracts `delta` from the current value. 135 | 136 | :param delta: The delta to subtract. 137 | ''' 138 | with self._lock.exclusive: 139 | self._value -= delta 140 | return self._value 141 | 142 | def get_and_subtract(self, delta): 143 | ''' 144 | Atomically subtracts `delta` from the current value and returns the 145 | old value. 146 | 147 | :param delta: The delta to subtract. 148 | ''' 149 | with self._lock.exclusive: 150 | oldval = self._value 151 | self._value -= delta 152 | return oldval 153 | 154 | 155 | class AtomicInteger(AtomicNumber): 156 | ''' 157 | An integer value which allows atomic manipulation semantics. 158 | ''' 159 | def __init__(self, value=0): 160 | super(AtomicInteger, self).__init__(value=value) 161 | 162 | def __setattr__(self, name, value): 163 | # Ensure the `_value` attribute is always an int. 164 | if name == '_value' and not isinstance(value, int): 165 | raise TypeError('_value must be of type int') 166 | 167 | super(AtomicInteger, self).__setattr__(name, value) 168 | 169 | 170 | class AtomicLong(AtomicNumber): 171 | ''' 172 | A long value which allows atomic manipulation semantics. 173 | ''' 174 | def __init__(self, value=long(0)): 175 | super(AtomicLong, self).__init__(value=value) 176 | 177 | def __setattr__(self, name, value): 178 | # Ensure the `_value` attribute is always a long. 179 | if name == '_value' and not isinstance(value, long): 180 | raise TypeError('_value must be of type long') 181 | 182 | super(AtomicLong, self).__setattr__(name, value) 183 | 184 | 185 | class AtomicFloat(AtomicNumber): 186 | ''' 187 | A float value which allows atomic manipulation semantics. 188 | ''' 189 | def __init__(self, value=float(0)): 190 | super(AtomicFloat, self).__init__(value=value) 191 | 192 | def __setattr__(self, name, value): 193 | # Ensure the `_value` attribute is always a float. 194 | if name == '_value' and not isinstance(value, float): 195 | raise TypeError('_value must be of type float') 196 | 197 | super(AtomicFloat, self).__setattr__(name, value) 198 | -------------------------------------------------------------------------------- /atomos/multiprocessing/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxcountryman/atomos/418746c69134efba3c4f999405afe9113dee4827/atomos/multiprocessing/__init__.py -------------------------------------------------------------------------------- /atomos/multiprocessing/atom.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | atomos.multiprocessing.atom 4 | 5 | Atom data type. 6 | ''' 7 | 8 | import atomos.atom 9 | import atomos.multiprocessing.atomic as atomic 10 | import atomos.util as util 11 | 12 | 13 | class Atom(atomos.atom.Atom): 14 | ''' 15 | Same as atomos.atom.Atom, except uses AtomicReference from 16 | atomos.multiprocessing.atomic. 17 | ''' 18 | def __init__(self, state): 19 | super(Atom, self).__init__(state) 20 | self._state = atomic.AtomicReference(state) 21 | 22 | def __repr__(self): 23 | return util.repr(__name__, self, self._state._proxy_value()) 24 | -------------------------------------------------------------------------------- /atomos/multiprocessing/atomic.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | atomos.multiprocessing.atomic 4 | 5 | Atomic primitives multiprocessing. 6 | ''' 7 | 8 | import types 9 | import ctypes 10 | import multiprocessing 11 | import multiprocessing.managers 12 | 13 | import six 14 | 15 | import atomos.util as util 16 | import atomos.atomic as atomic 17 | 18 | if six.PY3: 19 | long = int 20 | 21 | 22 | class _AtomicReference(atomic.AtomicReference): 23 | ''' 24 | A reference to an object which allows atomic manipulation semantics. 25 | 26 | AtomicReferences are particularlly useful when an object cannot otherwise 27 | be manipulated atomically. 28 | ''' 29 | def __init__(self, value=None): 30 | super(_AtomicReference, self).__init__(value=value) 31 | self._lock = util.ReadersWriterLockMultiprocessing() 32 | 33 | def __repr__(self): 34 | return util.repr(__name__, self, self._value) 35 | 36 | def _proxy_value(self): 37 | return self._value 38 | 39 | 40 | class AtomicManager(multiprocessing.managers.BaseManager): 41 | pass 42 | 43 | 44 | AtomicManager.register('AtomicReference', 45 | _AtomicReference, 46 | exposed=['get', 47 | 'set', 48 | 'get_and_set', 49 | 'compare_and_set', 50 | '_proxy_value', 51 | '__repr__']) 52 | 53 | # HACK: This is a bit of a hack. Essentially we need to run a manager which 54 | # specifically exposes the multiprocessing version of AtomicReference. If we 55 | # don't do this then memory cannot be shared between processes. 56 | _started = False 57 | if not _started: 58 | _manager = AtomicManager() 59 | _manager.start() 60 | AtomicReference = _manager.AtomicReference 61 | _started = True 62 | 63 | 64 | class AtomicCtypesReference(object): 65 | ''' 66 | A reference to an object which allows atomic manipulation semantics. 67 | 68 | AtomicCtypesReferences are particularlly useful when an object cannot 69 | otherwise be manipulated atomically. 70 | 71 | This only support ctypes data types. 72 | https://docs.python.org/3.4/library/ctypes.html#fundamental-data-types 73 | ''' 74 | def __init__(self, typecode_or_type=None, value=None): 75 | ''' 76 | Atomic reference 77 | 78 | :param typecode_or_type: The type of object allocated from shared 79 | memory. 80 | :param value: The default value. 81 | ''' 82 | self._typecode_or_type = typecode_or_type 83 | self._reference = multiprocessing.Value(self._typecode_or_type, value) 84 | 85 | def __repr__(self): 86 | return util.repr(__name__, self, self._reference) 87 | 88 | def get(self): 89 | ''' 90 | Returns the value. 91 | ''' 92 | with self._reference.get_lock(): 93 | return self._reference.value 94 | 95 | def set(self, value): 96 | ''' 97 | Atomically sets the value to `value`. 98 | 99 | :param value: The value to set. 100 | ''' 101 | with self._reference.get_lock(): 102 | self._reference.value = value 103 | return value 104 | 105 | def get_and_set(self, value): 106 | ''' 107 | Atomically sets the value to `value` and returns the old value. 108 | 109 | :param value: The value to set. 110 | ''' 111 | with self._reference.get_lock(): 112 | oldval = self._reference.value 113 | self._reference.value = value 114 | return oldval 115 | 116 | def compare_and_set(self, expect, update): 117 | ''' 118 | Atomically sets the value to `update` if the current value is equal to 119 | `expect`. 120 | 121 | :param expect: The expected current value. 122 | :param update: The value to set if and only if `expect` equals the 123 | current value. 124 | ''' 125 | with self._reference.get_lock(): 126 | if self._reference.value == expect: 127 | self._reference.value = update 128 | return True 129 | 130 | return False 131 | 132 | 133 | class AtomicBoolean(AtomicCtypesReference): 134 | ''' 135 | A boolean value whichs allows atomic manipulation semantics. 136 | ''' 137 | def __init__(self, value=False): 138 | super(AtomicBoolean, self).__init__(typecode_or_type=ctypes.c_bool, 139 | value=value) 140 | 141 | # We do not need a locked get since a boolean is not a complex data type. 142 | def get(self): 143 | ''' 144 | Returns the value. 145 | ''' 146 | return self._reference.value 147 | 148 | def __setattr__(self, name, value): 149 | # Ensure the `value` attribute is always a bool. 150 | if name == '_value' and not isinstance(value, types.BooleanType): 151 | raise TypeError('_value must be of type bool') 152 | 153 | super(AtomicBoolean, self).__setattr__(name, value) 154 | 155 | 156 | class AtomicNumber(AtomicCtypesReference): 157 | ''' 158 | AtomicNumber object super type. 159 | 160 | Contains common methods for AtomicInteger, AtomicLong, and AtomicFloat. 161 | ''' 162 | # We do not need a locked get since numbers are not complex data types. 163 | def get(self): 164 | ''' 165 | Returns the value. 166 | ''' 167 | return self._reference.value 168 | 169 | def add_and_get(self, delta): 170 | ''' 171 | Atomically adds `delta` to the current value. 172 | 173 | :param delta: The delta to add. 174 | ''' 175 | with self._reference.get_lock(): 176 | self._reference.value += delta 177 | return self._reference.value 178 | 179 | def get_and_add(self, delta): 180 | ''' 181 | Atomically adds `delta` to the current value and returns the old value. 182 | 183 | :param delta: The delta to add. 184 | ''' 185 | with self._reference.get_lock(): 186 | oldval = self._reference.value 187 | self._reference.value += delta 188 | return oldval 189 | 190 | def subtract_and_get(self, delta): 191 | ''' 192 | Atomically subtracts `delta` from the current value. 193 | 194 | :param delta: The delta to subtract. 195 | ''' 196 | with self._reference.get_lock(): 197 | self._reference.value -= delta 198 | return self._reference.value 199 | 200 | def get_and_subtract(self, delta): 201 | ''' 202 | Atomically subtracts `delta` from the current value and returns the 203 | old value. 204 | 205 | :param delta: The delta to subtract. 206 | ''' 207 | with self._reference.get_lock(): 208 | oldval = self._reference.value 209 | self._reference.value -= delta 210 | return oldval 211 | 212 | 213 | class AtomicInteger(AtomicNumber): 214 | ''' 215 | An integer value which allows atomic manipulation semantics. 216 | ''' 217 | def __init__(self, value=0): 218 | super(AtomicInteger, self).__init__(typecode_or_type=ctypes.c_int, 219 | value=value) 220 | 221 | def __setattr__(self, name, value): 222 | # Ensure the `_value` attribute is always an int. 223 | if name == '_value' and not isinstance(value, int): 224 | raise TypeError('_value must be of type int') 225 | 226 | super(AtomicInteger, self).__setattr__(name, value) 227 | 228 | 229 | class AtomicLong(AtomicNumber): 230 | ''' 231 | A long value which allows atomic manipulation semantics. 232 | ''' 233 | def __init__(self, value=long(0)): 234 | super(AtomicLong, self).__init__(typecode_or_type=ctypes.c_long, 235 | value=value) 236 | 237 | def __setattr__(self, name, value): 238 | # Ensure the `_value` attribute is always a long. 239 | if name == '_value' and not isinstance(value, long): 240 | raise TypeError('_value must be of type long') 241 | 242 | super(AtomicLong, self).__setattr__(name, value) 243 | 244 | 245 | class AtomicFloat(AtomicNumber): 246 | ''' 247 | A float value which allows atomic manipulation semantics. 248 | ''' 249 | def __init__(self, value=float(0)): 250 | super(AtomicFloat, self).__init__(typecode_or_type=ctypes.c_float, 251 | value=value) 252 | 253 | def __setattr__(self, name, value): 254 | # Ensure the `_value` attribute is always a float. 255 | if name == '_value' and not isinstance(value, float): 256 | raise TypeError('_value must be of type float') 257 | 258 | super(AtomicFloat, self).__setattr__(name, value) 259 | -------------------------------------------------------------------------------- /atomos/util.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | atomos.util 4 | 5 | Utility functions. 6 | ''' 7 | from __future__ import absolute_import 8 | import functools 9 | import threading 10 | from multiprocessing import Value, Lock 11 | 12 | 13 | def repr(module, instance, value): 14 | repr_fmt = '<{m}.{cls}({val}) object at {addr}>' 15 | return repr_fmt.format(m=module, 16 | cls=instance.__class__.__name__, 17 | val=value, 18 | addr=hex(id(instance))) 19 | 20 | 21 | def synchronized(fn): 22 | ''' 23 | A decorator which acquires a lock before attempting to execute its wrapped 24 | function. Releases the lock in a finally clause. 25 | 26 | :param fn: The function to wrap. 27 | ''' 28 | lock = threading.Lock() 29 | 30 | @functools.wraps(fn) 31 | def decorated(*args, **kwargs): 32 | lock.acquire() 33 | try: 34 | return fn(*args, **kwargs) 35 | finally: 36 | lock.release() 37 | 38 | return decorated 39 | 40 | 41 | class ReadersWriterLock(object): 42 | ''' 43 | A readers-writer lock. 44 | 45 | Provides exclusive locking on write while allowing for concurrent access 46 | on read. Useful for when a data structure cannot be updated atomically and 47 | therefore reads during a write could yield incorrect representations. 48 | 49 | To use, construct a new `ReadersWriterLock` instance. Use the shared 50 | property (i.e. the shared lock object) when the shared semantic is desired 51 | and the exclusive property (i.e. the exclusive lock object) when the 52 | exclusive semantic is desired. 53 | 54 | Lock objects are wrappers around `threading.Lock`. As a result, the normal 55 | usage patterns are valid. For example, a shared lock can be acquired like 56 | this:: 57 | 58 | >>> lock = ReadersWriterLock() 59 | >>> lock.shared.acquire() 60 | 61 | An exclusive lock can be acquired in a similar fashion, using the 62 | `exclusive` attribute instead. Both locks are also provisioned as context 63 | managers. Note that a difference in API here is that no blocking parameter 64 | should be provided. 65 | 66 | Readers-writer locks are meant to allow for a specific situation where a 67 | critical section should be visible to multiple readers so long as there is 68 | no writer. The latter case is simply an exclusive lock. However this does 69 | not allow for concurrent readers. 70 | 71 | To facilitate multiple readers, two "locks" are provided: a shared and 72 | exclusive lock. While the exclusive lock is not held, the shared lock may 73 | be acquired as many times as desired. However once the exclusive lock is 74 | obtained, attempts to acquire the read lock will block until the exclusive 75 | lock is released. 76 | 77 | Note that obtaining the write lock implies that there are no readers and in 78 | fact an attempt to acquire it will block until all the readers have 79 | released the lock. 80 | ''' 81 | def __init__(self): 82 | self._reader_lock = threading.Lock() 83 | self._writer_lock = threading.Lock() 84 | 85 | self._reader_count = 0 86 | 87 | class SharedLock(object): 88 | def acquire(inner): 89 | ''' 90 | Acquires the shared lock, prevents acquisition of the exclusive 91 | lock. 92 | ''' 93 | self._reader_lock.acquire() 94 | 95 | if self._reader_count == 0: 96 | self._writer_lock.acquire() 97 | 98 | try: 99 | self._reader_count += 1 100 | finally: 101 | self._reader_lock.release() 102 | 103 | def release(inner): 104 | ''' 105 | Releases the shared lock, allows acquisition of the exclusive 106 | lock. 107 | ''' 108 | self._reader_lock.acquire() 109 | 110 | try: 111 | self._reader_count -= 1 112 | finally: 113 | if self._reader_count == 0: 114 | self._writer_lock.release() 115 | 116 | self._reader_lock.release() 117 | 118 | def __enter__(inner): 119 | inner.acquire() 120 | return inner 121 | 122 | def __exit__(inner, exc_value, exc_type, tb): 123 | inner.release() 124 | 125 | self.shared = SharedLock() 126 | 127 | class ExclusiveLock(object): 128 | def acquire(inner): 129 | ''' 130 | Acquires the exclusive lock, prevents acquisition of the shared 131 | lock. 132 | ''' 133 | self._writer_lock.acquire() 134 | 135 | def release(inner): 136 | ''' 137 | Releases the exclusive lock, allows acquistion of the shared 138 | lock. 139 | ''' 140 | self._writer_lock.release() 141 | 142 | def __enter__(inner): 143 | inner.acquire() 144 | return inner 145 | 146 | def __exit__(inner, exc_value, exc_type, tb): 147 | inner.release() 148 | 149 | self.exclusive = ExclusiveLock() 150 | 151 | 152 | class ReadersWriterLockMultiprocessing(object): 153 | ''' 154 | A readers-writer lock multiprocessing. 155 | 156 | Works like ReadersWriterLock but uses multiprocessing Lock and Value. 157 | ''' 158 | def __init__(self): 159 | self._reader_lock = Lock() 160 | self._writer_lock = Lock() 161 | 162 | self._reader_count = Value('i') 163 | 164 | class SharedLock(object): 165 | def acquire(inner): 166 | ''' 167 | Acquires the shared lock, prevents acquisition of the exclusive 168 | lock. 169 | ''' 170 | self._reader_lock.acquire() 171 | 172 | if self._reader_count.value == 0: 173 | self._writer_lock.acquire() 174 | 175 | try: 176 | self._reader_count.value += 1 177 | finally: 178 | self._reader_lock.release() 179 | 180 | def release(inner): 181 | ''' 182 | Releases the shared lock, allows acquisition of the exclusive 183 | lock. 184 | ''' 185 | self._reader_lock.acquire() 186 | 187 | try: 188 | self._reader_count.value -= 1 189 | finally: 190 | if self._reader_count.value == 0: 191 | self._writer_lock.release() 192 | 193 | self._reader_lock.release() 194 | 195 | def __enter__(inner): 196 | inner.acquire() 197 | return inner 198 | 199 | def __exit__(inner, exc_value, exc_type, tb): 200 | inner.release() 201 | 202 | self.shared = SharedLock() 203 | 204 | class ExclusiveLock(object): 205 | def acquire(inner): 206 | ''' 207 | Acquires the exclusive lock, prevents acquisition of the shared 208 | lock. 209 | ''' 210 | self._writer_lock.acquire() 211 | 212 | def release(inner): 213 | ''' 214 | Releases the exclusive lock, allows acquistion of the shared 215 | lock. 216 | ''' 217 | self._writer_lock.release() 218 | 219 | def __enter__(inner): 220 | inner.acquire() 221 | return inner 222 | 223 | def __exit__(inner, exc_value, exc_type, tb): 224 | inner.release() 225 | 226 | self.exclusive = ExclusiveLock() 227 | -------------------------------------------------------------------------------- /docs/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxcountryman/atomos/418746c69134efba3c4f999405afe9113dee4827/docs/.DS_Store -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Atomos.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Atomos.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/Atomos" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Atomos" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /docs/_themes/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 by Armin Ronacher. 2 | 3 | Some rights reserved. 4 | 5 | Redistribution and use in source and binary forms of the theme, with or 6 | without modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above 13 | copyright notice, this list of conditions and the following 14 | disclaimer in the documentation and/or other materials provided 15 | with the distribution. 16 | 17 | * The names of the contributors may not be used to endorse or 18 | promote products derived from this software without specific 19 | prior written permission. 20 | 21 | We kindly ask you to only use these themes in an unmodified manner just 22 | for Flask and Flask-related products, not for unrelated projects. If you 23 | like the visual style and want to use it for your own projects, please 24 | consider making some larger changes to the themes (such as changing 25 | font faces, sizes, colors or margins). 26 | 27 | THIS THEME IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 28 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 31 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 | ARISING IN ANY WAY OUT OF THE USE OF THIS THEME, EVEN IF ADVISED OF THE 37 | POSSIBILITY OF SUCH DAMAGE. 38 | -------------------------------------------------------------------------------- /docs/_themes/README: -------------------------------------------------------------------------------- 1 | Flask Sphinx Styles 2 | =================== 3 | 4 | This repository contains sphinx styles for Flask and Flask related 5 | projects. To use this style in your Sphinx documentation, follow 6 | this guide: 7 | 8 | 1. put this folder as _themes into your docs folder. Alternatively 9 | you can also use git submodules to check out the contents there. 10 | 2. add this to your conf.py: 11 | 12 | sys.path.append(os.path.abspath('_themes')) 13 | html_theme_path = ['_themes'] 14 | html_theme = 'flask' 15 | 16 | The following themes exist: 17 | 18 | - 'flask' - the standard flask documentation theme for large 19 | projects 20 | - 'flask_small' - small one-page theme. Intended to be used by 21 | very small addon libraries for flask. 22 | 23 | The following options exist for the flask_small theme: 24 | 25 | [options] 26 | index_logo = '' filename of a picture in _static 27 | to be used as replacement for the 28 | h1 in the index.rst file. 29 | index_logo_height = 120px height of the index logo 30 | github_fork = '' repository name on github for the 31 | "fork me" badge 32 | -------------------------------------------------------------------------------- /docs/_themes/flask/layout.html: -------------------------------------------------------------------------------- 1 | {%- extends "basic/layout.html" %} 2 | {%- block extrahead %} 3 | {{ super() }} 4 | {% if theme_touch_icon %} 5 | 6 | {% endif %} 7 | 9 | {% endblock %} 10 | {%- block relbar2 %}{% endblock %} 11 | {%- block footer %} 12 | 16 | {%- endblock %} 17 | -------------------------------------------------------------------------------- /docs/_themes/flask/relations.html: -------------------------------------------------------------------------------- 1 |

Related Topics

2 | 20 | -------------------------------------------------------------------------------- /docs/_themes/flask/static/flasky.css_t: -------------------------------------------------------------------------------- 1 | /* 2 | * flasky.css_t 3 | * ~~~~~~~~~~~~ 4 | * 5 | * :copyright: Copyright 2010 by Armin Ronacher. 6 | * :license: Flask Design License, see LICENSE for details. 7 | */ 8 | 9 | {% set page_width = '940px' %} 10 | {% set sidebar_width = '220px' %} 11 | 12 | @import url("basic.css"); 13 | 14 | /* -- page layout ----------------------------------------------------------- */ 15 | 16 | body { 17 | font-family: 'Georgia', serif; 18 | font-size: 17px; 19 | background-color: white; 20 | color: #000; 21 | margin: 0; 22 | padding: 0; 23 | } 24 | 25 | div.document { 26 | width: {{ page_width }}; 27 | margin: 30px auto 0 auto; 28 | } 29 | 30 | div.documentwrapper { 31 | float: left; 32 | width: 100%; 33 | } 34 | 35 | div.bodywrapper { 36 | margin: 0 0 0 {{ sidebar_width }}; 37 | } 38 | 39 | div.sphinxsidebar { 40 | width: {{ sidebar_width }}; 41 | } 42 | 43 | hr { 44 | border: 1px solid #B1B4B6; 45 | } 46 | 47 | div.body { 48 | background-color: #ffffff; 49 | color: #3E4349; 50 | padding: 0 30px 0 30px; 51 | } 52 | 53 | img.floatingflask { 54 | padding: 0 0 10px 10px; 55 | float: right; 56 | } 57 | 58 | div.footer { 59 | width: {{ page_width }}; 60 | margin: 20px auto 30px auto; 61 | font-size: 14px; 62 | color: #888; 63 | text-align: right; 64 | } 65 | 66 | div.footer a { 67 | color: #888; 68 | } 69 | 70 | div.related { 71 | display: none; 72 | } 73 | 74 | div.sphinxsidebar a { 75 | color: #444; 76 | text-decoration: none; 77 | border-bottom: 1px dotted #999; 78 | } 79 | 80 | div.sphinxsidebar a:hover { 81 | border-bottom: 1px solid #999; 82 | } 83 | 84 | div.sphinxsidebar { 85 | font-size: 14px; 86 | line-height: 1.5; 87 | } 88 | 89 | div.sphinxsidebarwrapper { 90 | padding: 18px 10px; 91 | } 92 | 93 | div.sphinxsidebarwrapper p.logo { 94 | padding: 0 0 20px 0; 95 | margin: 0; 96 | text-align: center; 97 | } 98 | 99 | div.sphinxsidebar h3, 100 | div.sphinxsidebar h4 { 101 | font-family: 'Garamond', 'Georgia', serif; 102 | color: #444; 103 | font-size: 24px; 104 | font-weight: normal; 105 | margin: 0 0 5px 0; 106 | padding: 0; 107 | } 108 | 109 | div.sphinxsidebar h4 { 110 | font-size: 20px; 111 | } 112 | 113 | div.sphinxsidebar h3 a { 114 | color: #444; 115 | } 116 | 117 | div.sphinxsidebar p.logo a, 118 | div.sphinxsidebar h3 a, 119 | div.sphinxsidebar p.logo a:hover, 120 | div.sphinxsidebar h3 a:hover { 121 | border: none; 122 | } 123 | 124 | div.sphinxsidebar p { 125 | color: #555; 126 | margin: 10px 0; 127 | } 128 | 129 | div.sphinxsidebar ul { 130 | margin: 10px 0; 131 | padding: 0; 132 | color: #000; 133 | } 134 | 135 | div.sphinxsidebar input { 136 | border: 1px solid #ccc; 137 | font-family: 'Georgia', serif; 138 | font-size: 1em; 139 | } 140 | 141 | /* -- body styles ----------------------------------------------------------- */ 142 | 143 | a { 144 | color: #004B6B; 145 | text-decoration: underline; 146 | } 147 | 148 | a:hover { 149 | color: #6D4100; 150 | text-decoration: underline; 151 | } 152 | 153 | div.body h1, 154 | div.body h2, 155 | div.body h3, 156 | div.body h4, 157 | div.body h5, 158 | div.body h6 { 159 | font-family: 'Garamond', 'Georgia', serif; 160 | font-weight: normal; 161 | margin: 30px 0px 10px 0px; 162 | padding: 0; 163 | } 164 | 165 | div.body h1 { margin-top: 0; padding-top: 0; font-size: 240%; } 166 | div.body h2 { font-size: 180%; } 167 | div.body h3 { font-size: 150%; } 168 | div.body h4 { font-size: 130%; } 169 | div.body h5 { font-size: 100%; } 170 | div.body h6 { font-size: 100%; } 171 | 172 | a.headerlink { 173 | color: #ddd; 174 | padding: 0 4px; 175 | text-decoration: none; 176 | } 177 | 178 | a.headerlink:hover { 179 | color: #444; 180 | background: #eaeaea; 181 | } 182 | 183 | div.body p, div.body dd, div.body li { 184 | line-height: 1.4em; 185 | } 186 | 187 | div.admonition { 188 | background: #fafafa; 189 | margin: 20px -30px; 190 | padding: 10px 30px; 191 | border-top: 1px solid #ccc; 192 | border-bottom: 1px solid #ccc; 193 | } 194 | 195 | div.admonition tt.xref, div.admonition a tt { 196 | border-bottom: 1px solid #fafafa; 197 | } 198 | 199 | dd div.admonition { 200 | margin-left: -60px; 201 | padding-left: 60px; 202 | } 203 | 204 | div.admonition p.admonition-title { 205 | font-family: 'Garamond', 'Georgia', serif; 206 | font-weight: normal; 207 | font-size: 24px; 208 | margin: 0 0 10px 0; 209 | padding: 0; 210 | line-height: 1; 211 | } 212 | 213 | div.admonition p.last { 214 | margin-bottom: 0; 215 | } 216 | 217 | div.highlight { 218 | background-color: white; 219 | } 220 | 221 | dt:target, .highlight { 222 | background: #FAF3E8; 223 | } 224 | 225 | div.note { 226 | background-color: #eee; 227 | border: 1px solid #ccc; 228 | } 229 | 230 | div.seealso { 231 | background-color: #ffc; 232 | border: 1px solid #ff6; 233 | } 234 | 235 | div.topic { 236 | background-color: #eee; 237 | } 238 | 239 | p.admonition-title { 240 | display: inline; 241 | } 242 | 243 | p.admonition-title:after { 244 | content: ":"; 245 | } 246 | 247 | pre, tt { 248 | font-family: 'Consolas', 'Menlo', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace; 249 | font-size: 0.9em; 250 | } 251 | 252 | img.screenshot { 253 | } 254 | 255 | tt.descname, tt.descclassname { 256 | font-size: 0.95em; 257 | } 258 | 259 | tt.descname { 260 | padding-right: 0.08em; 261 | } 262 | 263 | img.screenshot { 264 | -moz-box-shadow: 2px 2px 4px #eee; 265 | -webkit-box-shadow: 2px 2px 4px #eee; 266 | box-shadow: 2px 2px 4px #eee; 267 | } 268 | 269 | table.docutils { 270 | border: 1px solid #888; 271 | -moz-box-shadow: 2px 2px 4px #eee; 272 | -webkit-box-shadow: 2px 2px 4px #eee; 273 | box-shadow: 2px 2px 4px #eee; 274 | } 275 | 276 | table.docutils td, table.docutils th { 277 | border: 1px solid #888; 278 | padding: 0.25em 0.7em; 279 | } 280 | 281 | table.field-list, table.footnote { 282 | border: none; 283 | -moz-box-shadow: none; 284 | -webkit-box-shadow: none; 285 | box-shadow: none; 286 | } 287 | 288 | table.footnote { 289 | margin: 15px 0; 290 | width: 100%; 291 | border: 1px solid #eee; 292 | background: #fdfdfd; 293 | font-size: 0.9em; 294 | } 295 | 296 | table.footnote + table.footnote { 297 | margin-top: -15px; 298 | border-top: none; 299 | } 300 | 301 | table.field-list th { 302 | padding: 0 0.8em 0 0; 303 | } 304 | 305 | table.field-list td { 306 | padding: 0; 307 | } 308 | 309 | table.footnote td.label { 310 | width: 0px; 311 | padding: 0.3em 0 0.3em 0.5em; 312 | } 313 | 314 | table.footnote td { 315 | padding: 0.3em 0.5em; 316 | } 317 | 318 | dl { 319 | margin: 0; 320 | padding: 0; 321 | } 322 | 323 | dl dd { 324 | margin-left: 30px; 325 | } 326 | 327 | blockquote { 328 | margin: 0 0 0 30px; 329 | padding: 0; 330 | } 331 | 332 | ul, ol { 333 | margin: 10px 0 10px 30px; 334 | padding: 0; 335 | } 336 | 337 | pre { 338 | background: #eee; 339 | padding: 7px 30px; 340 | margin: 15px -30px; 341 | line-height: 1.3em; 342 | } 343 | 344 | dl pre, blockquote pre, li pre { 345 | margin-left: -60px; 346 | padding-left: 60px; 347 | } 348 | 349 | dl dl pre { 350 | margin-left: -90px; 351 | padding-left: 90px; 352 | } 353 | 354 | tt { 355 | background-color: #ecf0f3; 356 | color: #222; 357 | /* padding: 1px 2px; */ 358 | } 359 | 360 | tt.xref, a tt { 361 | background-color: #FBFBFB; 362 | border-bottom: 1px solid white; 363 | } 364 | 365 | a.reference { 366 | text-decoration: none; 367 | border-bottom: 1px dotted #004B6B; 368 | } 369 | 370 | a.reference:hover { 371 | border-bottom: 1px solid #6D4100; 372 | } 373 | 374 | a.footnote-reference { 375 | text-decoration: none; 376 | font-size: 0.7em; 377 | vertical-align: top; 378 | border-bottom: 1px dotted #004B6B; 379 | } 380 | 381 | a.footnote-reference:hover { 382 | border-bottom: 1px solid #6D4100; 383 | } 384 | 385 | a:hover tt { 386 | background: #EEE; 387 | } 388 | -------------------------------------------------------------------------------- /docs/_themes/flask/static/small_flask.css: -------------------------------------------------------------------------------- 1 | /* 2 | * small_flask.css_t 3 | * ~~~~~~~~~~~~~~~~~ 4 | * 5 | * :copyright: Copyright 2010 by Armin Ronacher. 6 | * :license: Flask Design License, see LICENSE for details. 7 | */ 8 | 9 | body { 10 | margin: 0; 11 | padding: 20px 30px; 12 | } 13 | 14 | div.documentwrapper { 15 | float: none; 16 | background: white; 17 | } 18 | 19 | div.sphinxsidebar { 20 | display: block; 21 | float: none; 22 | width: 102.5%; 23 | margin: 50px -30px -20px -30px; 24 | padding: 10px 20px; 25 | background: #333; 26 | color: white; 27 | } 28 | 29 | div.sphinxsidebar h3, div.sphinxsidebar h4, div.sphinxsidebar p, 30 | div.sphinxsidebar h3 a { 31 | color: white; 32 | } 33 | 34 | div.sphinxsidebar a { 35 | color: #aaa; 36 | } 37 | 38 | div.sphinxsidebar p.logo { 39 | display: none; 40 | } 41 | 42 | div.document { 43 | width: 100%; 44 | margin: 0; 45 | } 46 | 47 | div.related { 48 | display: block; 49 | margin: 0; 50 | padding: 10px 0 20px 0; 51 | } 52 | 53 | div.related ul, 54 | div.related ul li { 55 | margin: 0; 56 | padding: 0; 57 | } 58 | 59 | div.footer { 60 | display: none; 61 | } 62 | 63 | div.bodywrapper { 64 | margin: 0; 65 | } 66 | 67 | div.body { 68 | min-height: 0; 69 | padding: 0; 70 | } 71 | -------------------------------------------------------------------------------- /docs/_themes/flask/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = basic 3 | stylesheet = flasky.css 4 | pygments_style = flask_theme_support.FlaskyStyle 5 | 6 | [options] 7 | touch_icon = 8 | -------------------------------------------------------------------------------- /docs/_themes/flask_small/layout.html: -------------------------------------------------------------------------------- 1 | {% extends "basic/layout.html" %} 2 | {% block header %} 3 | {{ super() }} 4 | {% if pagename == 'index' %} 5 |
6 | {% endif %} 7 | {% endblock %} 8 | {% block footer %} 9 | {% if pagename == 'index' %} 10 |
11 | {% endif %} 12 | {% endblock %} 13 | {# do not display relbars #} 14 | {% block relbar1 %}{% endblock %} 15 | {% block relbar2 %} 16 | {% if theme_github_fork %} 17 | Fork me on GitHub 19 | {% endif %} 20 | {% endblock %} 21 | {% block sidebar1 %}{% endblock %} 22 | {% block sidebar2 %}{% endblock %} 23 | -------------------------------------------------------------------------------- /docs/_themes/flask_small/static/flasky.css_t: -------------------------------------------------------------------------------- 1 | /* 2 | * flasky.css_t 3 | * ~~~~~~~~~~~~ 4 | * 5 | * Sphinx stylesheet -- flasky theme based on nature theme. 6 | * 7 | * :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS. 8 | * :license: BSD, see LICENSE for details. 9 | * 10 | */ 11 | 12 | @import url("basic.css"); 13 | 14 | /* -- page layout ----------------------------------------------------------- */ 15 | 16 | body { 17 | font-family: 'Georgia', serif; 18 | font-size: 17px; 19 | color: #000; 20 | background: white; 21 | margin: 0; 22 | padding: 0; 23 | } 24 | 25 | div.documentwrapper { 26 | float: left; 27 | width: 100%; 28 | } 29 | 30 | div.bodywrapper { 31 | margin: 40px auto 0 auto; 32 | width: 700px; 33 | } 34 | 35 | hr { 36 | border: 1px solid #B1B4B6; 37 | } 38 | 39 | div.body { 40 | background-color: #ffffff; 41 | color: #3E4349; 42 | padding: 0 30px 30px 30px; 43 | } 44 | 45 | img.floatingflask { 46 | padding: 0 0 10px 10px; 47 | float: right; 48 | } 49 | 50 | div.footer { 51 | text-align: right; 52 | color: #888; 53 | padding: 10px; 54 | font-size: 14px; 55 | width: 650px; 56 | margin: 0 auto 40px auto; 57 | } 58 | 59 | div.footer a { 60 | color: #888; 61 | text-decoration: underline; 62 | } 63 | 64 | div.related { 65 | line-height: 32px; 66 | color: #888; 67 | } 68 | 69 | div.related ul { 70 | padding: 0 0 0 10px; 71 | } 72 | 73 | div.related a { 74 | color: #444; 75 | } 76 | 77 | /* -- body styles ----------------------------------------------------------- */ 78 | 79 | a { 80 | color: #004B6B; 81 | text-decoration: underline; 82 | } 83 | 84 | a:hover { 85 | color: #6D4100; 86 | text-decoration: underline; 87 | } 88 | 89 | div.body { 90 | padding-bottom: 40px; /* saved for footer */ 91 | } 92 | 93 | div.body h1, 94 | div.body h2, 95 | div.body h3, 96 | div.body h4, 97 | div.body h5, 98 | div.body h6 { 99 | font-family: 'Garamond', 'Georgia', serif; 100 | font-weight: normal; 101 | margin: 30px 0px 10px 0px; 102 | padding: 0; 103 | } 104 | 105 | {% if theme_index_logo %} 106 | div.indexwrapper h1 { 107 | text-indent: -999999px; 108 | background: url({{ theme_index_logo }}) no-repeat center center; 109 | height: {{ theme_index_logo_height }}; 110 | } 111 | {% endif %} 112 | 113 | div.body h2 { font-size: 180%; } 114 | div.body h3 { font-size: 150%; } 115 | div.body h4 { font-size: 130%; } 116 | div.body h5 { font-size: 100%; } 117 | div.body h6 { font-size: 100%; } 118 | 119 | a.headerlink { 120 | color: white; 121 | padding: 0 4px; 122 | text-decoration: none; 123 | } 124 | 125 | a.headerlink:hover { 126 | color: #444; 127 | background: #eaeaea; 128 | } 129 | 130 | div.body p, div.body dd, div.body li { 131 | line-height: 1.4em; 132 | } 133 | 134 | div.admonition { 135 | background: #fafafa; 136 | margin: 20px -30px; 137 | padding: 10px 30px; 138 | border-top: 1px solid #ccc; 139 | border-bottom: 1px solid #ccc; 140 | } 141 | 142 | div.admonition p.admonition-title { 143 | font-family: 'Garamond', 'Georgia', serif; 144 | font-weight: normal; 145 | font-size: 24px; 146 | margin: 0 0 10px 0; 147 | padding: 0; 148 | line-height: 1; 149 | } 150 | 151 | div.admonition p.last { 152 | margin-bottom: 0; 153 | } 154 | 155 | div.highlight{ 156 | background-color: white; 157 | } 158 | 159 | dt:target, .highlight { 160 | background: #FAF3E8; 161 | } 162 | 163 | div.note { 164 | background-color: #eee; 165 | border: 1px solid #ccc; 166 | } 167 | 168 | div.seealso { 169 | background-color: #ffc; 170 | border: 1px solid #ff6; 171 | } 172 | 173 | div.topic { 174 | background-color: #eee; 175 | } 176 | 177 | div.warning { 178 | background-color: #ffe4e4; 179 | border: 1px solid #f66; 180 | } 181 | 182 | p.admonition-title { 183 | display: inline; 184 | } 185 | 186 | p.admonition-title:after { 187 | content: ":"; 188 | } 189 | 190 | pre, tt { 191 | font-family: 'Consolas', 'Menlo', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace; 192 | font-size: 0.85em; 193 | } 194 | 195 | img.screenshot { 196 | } 197 | 198 | tt.descname, tt.descclassname { 199 | font-size: 0.95em; 200 | } 201 | 202 | tt.descname { 203 | padding-right: 0.08em; 204 | } 205 | 206 | img.screenshot { 207 | -moz-box-shadow: 2px 2px 4px #eee; 208 | -webkit-box-shadow: 2px 2px 4px #eee; 209 | box-shadow: 2px 2px 4px #eee; 210 | } 211 | 212 | table.docutils { 213 | border: 1px solid #888; 214 | -moz-box-shadow: 2px 2px 4px #eee; 215 | -webkit-box-shadow: 2px 2px 4px #eee; 216 | box-shadow: 2px 2px 4px #eee; 217 | } 218 | 219 | table.docutils td, table.docutils th { 220 | border: 1px solid #888; 221 | padding: 0.25em 0.7em; 222 | } 223 | 224 | table.field-list, table.footnote { 225 | border: none; 226 | -moz-box-shadow: none; 227 | -webkit-box-shadow: none; 228 | box-shadow: none; 229 | } 230 | 231 | table.footnote { 232 | margin: 15px 0; 233 | width: 100%; 234 | border: 1px solid #eee; 235 | } 236 | 237 | table.field-list th { 238 | padding: 0 0.8em 0 0; 239 | } 240 | 241 | table.field-list td { 242 | padding: 0; 243 | } 244 | 245 | table.footnote td { 246 | padding: 0.5em; 247 | } 248 | 249 | dl { 250 | margin: 0; 251 | padding: 0; 252 | } 253 | 254 | dl dd { 255 | margin-left: 30px; 256 | } 257 | 258 | pre { 259 | padding: 0; 260 | margin: 15px -30px; 261 | padding: 8px; 262 | line-height: 1.3em; 263 | padding: 7px 30px; 264 | background: #eee; 265 | border-radius: 2px; 266 | -moz-border-radius: 2px; 267 | -webkit-border-radius: 2px; 268 | } 269 | 270 | dl pre { 271 | margin-left: -60px; 272 | padding-left: 60px; 273 | } 274 | 275 | tt { 276 | background-color: #ecf0f3; 277 | color: #222; 278 | /* padding: 1px 2px; */ 279 | } 280 | 281 | tt.xref, a tt { 282 | background-color: #FBFBFB; 283 | } 284 | 285 | a:hover tt { 286 | background: #EEE; 287 | } 288 | -------------------------------------------------------------------------------- /docs/_themes/flask_small/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = basic 3 | stylesheet = flasky.css 4 | nosidebar = true 5 | pygments_style = flask_theme_support.FlaskyStyle 6 | 7 | [options] 8 | index_logo = '' 9 | index_logo_height = 120px 10 | github_fork = '' 11 | -------------------------------------------------------------------------------- /docs/_themes/flask_theme_support.py: -------------------------------------------------------------------------------- 1 | # flasky extensions. flasky pygments style based on tango style 2 | from pygments.style import Style 3 | from pygments.token import Keyword, Name, Comment, String, Error, \ 4 | Number, Operator, Generic, Whitespace, Punctuation, Other, Literal 5 | 6 | 7 | class FlaskyStyle(Style): 8 | background_color = "#f8f8f8" 9 | default_style = "" 10 | 11 | styles = { 12 | # No corresponding class for the following: 13 | #Text: "", # class: '' 14 | Whitespace: "underline #f8f8f8", # class: 'w' 15 | Error: "#a40000 border:#ef2929", # class: 'err' 16 | Other: "#000000", # class 'x' 17 | 18 | Comment: "italic #8f5902", # class: 'c' 19 | Comment.Preproc: "noitalic", # class: 'cp' 20 | 21 | Keyword: "bold #004461", # class: 'k' 22 | Keyword.Constant: "bold #004461", # class: 'kc' 23 | Keyword.Declaration: "bold #004461", # class: 'kd' 24 | Keyword.Namespace: "bold #004461", # class: 'kn' 25 | Keyword.Pseudo: "bold #004461", # class: 'kp' 26 | Keyword.Reserved: "bold #004461", # class: 'kr' 27 | Keyword.Type: "bold #004461", # class: 'kt' 28 | 29 | Operator: "#582800", # class: 'o' 30 | Operator.Word: "bold #004461", # class: 'ow' - like keywords 31 | 32 | Punctuation: "bold #000000", # class: 'p' 33 | 34 | # because special names such as Name.Class, Name.Function, etc. 35 | # are not recognized as such later in the parsing, we choose them 36 | # to look the same as ordinary variables. 37 | Name: "#000000", # class: 'n' 38 | Name.Attribute: "#c4a000", # class: 'na' - to be revised 39 | Name.Builtin: "#004461", # class: 'nb' 40 | Name.Builtin.Pseudo: "#3465a4", # class: 'bp' 41 | Name.Class: "#000000", # class: 'nc' - to be revised 42 | Name.Constant: "#000000", # class: 'no' - to be revised 43 | Name.Decorator: "#888", # class: 'nd' - to be revised 44 | Name.Entity: "#ce5c00", # class: 'ni' 45 | Name.Exception: "bold #cc0000", # class: 'ne' 46 | Name.Function: "#000000", # class: 'nf' 47 | Name.Property: "#000000", # class: 'py' 48 | Name.Label: "#f57900", # class: 'nl' 49 | Name.Namespace: "#000000", # class: 'nn' - to be revised 50 | Name.Other: "#000000", # class: 'nx' 51 | Name.Tag: "bold #004461", # class: 'nt' - like a keyword 52 | Name.Variable: "#000000", # class: 'nv' - to be revised 53 | Name.Variable.Class: "#000000", # class: 'vc' - to be revised 54 | Name.Variable.Global: "#000000", # class: 'vg' - to be revised 55 | Name.Variable.Instance: "#000000", # class: 'vi' - to be revised 56 | 57 | Number: "#990000", # class: 'm' 58 | 59 | Literal: "#000000", # class: 'l' 60 | Literal.Date: "#000000", # class: 'ld' 61 | 62 | String: "#4e9a06", # class: 's' 63 | String.Backtick: "#4e9a06", # class: 'sb' 64 | String.Char: "#4e9a06", # class: 'sc' 65 | String.Doc: "italic #8f5902", # class: 'sd' - like a comment 66 | String.Double: "#4e9a06", # class: 's2' 67 | String.Escape: "#4e9a06", # class: 'se' 68 | String.Heredoc: "#4e9a06", # class: 'sh' 69 | String.Interpol: "#4e9a06", # class: 'si' 70 | String.Other: "#4e9a06", # class: 'sx' 71 | String.Regex: "#4e9a06", # class: 'sr' 72 | String.Single: "#4e9a06", # class: 's1' 73 | String.Symbol: "#4e9a06", # class: 'ss' 74 | 75 | Generic: "#000000", # class: 'g' 76 | Generic.Deleted: "#a40000", # class: 'gd' 77 | Generic.Emph: "italic #000000", # class: 'ge' 78 | Generic.Error: "#ef2929", # class: 'gr' 79 | Generic.Heading: "bold #000080", # class: 'gh' 80 | Generic.Inserted: "#00A000", # class: 'gi' 81 | Generic.Output: "#888", # class: 'go' 82 | Generic.Prompt: "#745334", # class: 'gp' 83 | Generic.Strong: "bold #000000", # class: 'gs' 84 | Generic.Subheading: "bold #800080", # class: 'gu' 85 | Generic.Traceback: "bold #a40000", # class: 'gt' 86 | } 87 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Atomos documentation build configuration file, created by 4 | # sphinx-quickstart on Fri Oct 31 15:05:54 2014. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys 16 | import os 17 | 18 | # If extensions (or modules to document with autodoc) are in another directory, 19 | # add these directories to sys.path here. If the directory is relative to the 20 | # documentation root, use os.path.abspath to make it absolute, like shown here. 21 | sys.path.insert(0, os.path.abspath('.')) 22 | sys.path.insert(0, os.path.abspath('..')) 23 | 24 | # -- General configuration ------------------------------------------------ 25 | 26 | # If your documentation needs a minimal Sphinx version, state it here. 27 | #needs_sphinx = '1.0' 28 | 29 | # Add any Sphinx extension module names here, as strings. They can be 30 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 31 | # ones. 32 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx.ext.viewcode'] 33 | 34 | # Add any paths that contain templates here, relative to this directory. 35 | templates_path = ['_templates'] 36 | 37 | # The suffix of source filenames. 38 | source_suffix = '.rst' 39 | 40 | # The encoding of source files. 41 | #source_encoding = 'utf-8-sig' 42 | 43 | # The master toctree document. 44 | master_doc = 'index' 45 | 46 | # General information about the project. 47 | project = u'Atomos' 48 | copyright = u'2015, Max Countryman' 49 | 50 | from atomos.__about__ import __version__ 51 | # The version info for the project you're documenting, acts as replacement for 52 | # |version| and |release|, also used in various other places throughout the 53 | # built documents. 54 | # 55 | # The short X.Y version. 56 | version = __version__ 57 | # The full version, including alpha/beta/rc tags. 58 | release = __version__ 59 | 60 | # The language for content autogenerated by Sphinx. Refer to documentation 61 | # for a list of supported languages. 62 | #language = None 63 | 64 | # There are two options for replacing |today|: either, you set today to some 65 | # non-false value, then it is used: 66 | #today = '' 67 | # Else, today_fmt is used as the format for a strftime call. 68 | #today_fmt = '%B %d, %Y' 69 | 70 | # List of patterns, relative to source directory, that match files and 71 | # directories to ignore when looking for source files. 72 | exclude_patterns = ['_build'] 73 | 74 | # The reST default role (used for this markup: `text`) to use for all 75 | # documents. 76 | #default_role = None 77 | 78 | # If true, '()' will be appended to :func: etc. cross-reference text. 79 | #add_function_parentheses = True 80 | 81 | # If true, the current module name will be prepended to all description 82 | # unit titles (such as .. function::). 83 | #add_module_names = True 84 | 85 | # If true, sectionauthor and moduleauthor directives will be shown in the 86 | # output. They are ignored by default. 87 | #show_authors = False 88 | 89 | # The name of the Pygments (syntax highlighting) style to use. 90 | pygments_style = 'sphinx' 91 | 92 | # A list of ignored prefixes for module index sorting. 93 | #modindex_common_prefix = [] 94 | 95 | # If true, keep warnings as "system message" paragraphs in the built documents. 96 | #keep_warnings = False 97 | 98 | 99 | # -- Options for HTML output ---------------------------------------------- 100 | 101 | # The theme to use for HTML and HTML Help pages. See the documentation for 102 | # a list of builtin themes. 103 | html_theme = 'flask_small' 104 | 105 | # Theme options are theme-specific and customize the look and feel of a theme 106 | # further. For a list of options available for each theme, see the 107 | # documentation. 108 | html_theme_options = dict(github_fork='maxcountryman/atomos', index_logo=False) 109 | 110 | # Add any paths that contain custom themes here, relative to this directory. 111 | html_theme_path = ['_themes'] 112 | 113 | # The name for this set of Sphinx documents. If None, it defaults to 114 | # " v documentation". 115 | #html_title = None 116 | 117 | # A shorter title for the navigation bar. Default is the same as html_title. 118 | #html_short_title = None 119 | 120 | # The name of an image file (relative to this directory) to place at the top 121 | # of the sidebar. 122 | #html_logo = None 123 | 124 | # The name of an image file (within the static path) to use as favicon of the 125 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 126 | # pixels large. 127 | #html_favicon = None 128 | 129 | # Add any paths that contain custom static files (such as style sheets) here, 130 | # relative to this directory. They are copied after the builtin static files, 131 | # so a file named "default.css" will overwrite the builtin "default.css". 132 | html_static_path = ['_static'] 133 | 134 | # Add any extra paths that contain custom files (such as robots.txt or 135 | # .htaccess) here, relative to this directory. These files are copied 136 | # directly to the root of the documentation. 137 | #html_extra_path = [] 138 | 139 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 140 | # using the given strftime format. 141 | #html_last_updated_fmt = '%b %d, %Y' 142 | 143 | # If true, SmartyPants will be used to convert quotes and dashes to 144 | # typographically correct entities. 145 | #html_use_smartypants = True 146 | 147 | # Custom sidebar templates, maps document names to template names. 148 | #html_sidebars = {} 149 | 150 | # Additional templates that should be rendered to pages, maps page names to 151 | # template names. 152 | #html_additional_pages = {} 153 | 154 | # If false, no module index is generated. 155 | #html_domain_indices = True 156 | 157 | # If false, no index is generated. 158 | #html_use_index = True 159 | 160 | # If true, the index is split into individual pages for each letter. 161 | #html_split_index = False 162 | 163 | # If true, links to the reST sources are added to the pages. 164 | #html_show_sourcelink = True 165 | 166 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 167 | #html_show_sphinx = True 168 | 169 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 170 | #html_show_copyright = True 171 | 172 | # If true, an OpenSearch description file will be output, and all pages will 173 | # contain a tag referring to it. The value of this option must be the 174 | # base URL from which the finished HTML is served. 175 | #html_use_opensearch = '' 176 | 177 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 178 | #html_file_suffix = None 179 | 180 | # Output file base name for HTML help builder. 181 | htmlhelp_basename = 'Atomosdoc' 182 | 183 | 184 | # -- Options for LaTeX output --------------------------------------------- 185 | 186 | latex_elements = { 187 | # The paper size ('letterpaper' or 'a4paper'). 188 | #'papersize': 'letterpaper', 189 | 190 | # The font size ('10pt', '11pt' or '12pt'). 191 | #'pointsize': '10pt', 192 | 193 | # Additional stuff for the LaTeX preamble. 194 | #'preamble': '', 195 | } 196 | 197 | # Grouping the document tree into LaTeX files. List of tuples 198 | # (source start file, target name, title, 199 | # author, documentclass [howto, manual, or own class]). 200 | latex_documents = [ 201 | ('index', 'Atomos.tex', u'Atomos Documentation', 202 | u'Max Countryman', 'manual'), 203 | ] 204 | 205 | # The name of an image file (relative to this directory) to place at the top of 206 | # the title page. 207 | #latex_logo = None 208 | 209 | # For "manual" documents, if this is true, then toplevel headings are parts, 210 | # not chapters. 211 | #latex_use_parts = False 212 | 213 | # If true, show page references after internal links. 214 | #latex_show_pagerefs = False 215 | 216 | # If true, show URL addresses after external links. 217 | #latex_show_urls = False 218 | 219 | # Documents to append as an appendix to all manuals. 220 | #latex_appendices = [] 221 | 222 | # If false, no module index is generated. 223 | #latex_domain_indices = True 224 | 225 | 226 | # -- Options for manual page output --------------------------------------- 227 | 228 | # One entry per manual page. List of tuples 229 | # (source start file, name, description, authors, manual section). 230 | man_pages = [ 231 | ('index', 'atomos', u'Atomos Documentation', 232 | [u'Max Countryman'], 1) 233 | ] 234 | 235 | # If true, show URL addresses after external links. 236 | #man_show_urls = False 237 | 238 | 239 | # -- Options for Texinfo output ------------------------------------------- 240 | 241 | # Grouping the document tree into Texinfo files. List of tuples 242 | # (source start file, target name, title, author, 243 | # dir menu entry, description, category) 244 | texinfo_documents = [ 245 | ('index', 'Atomos', u'Atomos Documentation', 246 | u'Max Countryman', 'Atomos', 'One line description of project.', 247 | 'Miscellaneous'), 248 | ] 249 | 250 | # Documents to append as an appendix to all manuals. 251 | #texinfo_appendices = [] 252 | 253 | # If false, no module index is generated. 254 | #texinfo_domain_indices = True 255 | 256 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 257 | #texinfo_show_urls = 'footnote' 258 | 259 | # If true, do not generate a @detailmenu in the "Top" node's menu. 260 | #texinfo_no_detailmenu = False 261 | 262 | intersphinx_mapping = {'http://docs.python.org/': None} 263 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | :orphan: 2 | 3 | ====== 4 | Atomos 5 | ====== 6 | 7 | .. module:: atomos 8 | 9 | Atomic primitives for Python. 10 | 11 | Atomos is a library of atomic primitives, inspired by Java's java.util.concurrent.atomic. It provides atomic types for bools, ints, longs, and floats as well as a generalized object wrapper. In addition, it introduces `atoms`_, a concept Clojure programmers will be familiar with. 12 | 13 | .. _atoms: http://clojure.org/atoms 14 | 15 | Installation 16 | ============ 17 | 18 | Atomos is available via PyPI. :: 19 | 20 | $ pip install atomos 21 | 22 | Usage 23 | ===== 24 | 25 | A short tutorial is presented in the `README`_. 26 | 27 | .. _README: https://github.com/maxcountryman/atomos#usage 28 | 29 | API 30 | === 31 | .. autoclass:: atomos.atom.Atom 32 | :members: 33 | 34 | .. autoclass:: atomos.atom.ARef 35 | :members: 36 | 37 | .. autoclass:: atomos.atomic.AtomicReference 38 | :members: 39 | 40 | .. autoclass:: atomos.atomic.AtomicBoolean 41 | :members: 42 | 43 | .. autoclass:: atomos.atomic.AtomicInteger 44 | :members: 45 | 46 | .. autoclass:: atomos.atomic.AtomicLong 47 | :members: 48 | 49 | .. autoclass:: atomos.atomic.AtomicFloat 50 | :members: 51 | 52 | API Multiprocessing 53 | =================== 54 | .. autoclass:: atomos.multiprocessing.atomic.AtomicReference 55 | :members: 56 | 57 | .. autoclass:: atomos.multiprocessing.atomic.AtomicBoolean 58 | :members: 59 | 60 | .. autoclass:: atomos.multiprocessing.atomic.AtomicInteger 61 | :members: 62 | 63 | .. autoclass:: atomos.multiprocessing.atomic.AtomicLong 64 | :members: 65 | 66 | .. autoclass:: atomos.multiprocessing.atomic.AtomicFloat 67 | :members: 68 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | Atomos 4 | ------ 5 | 6 | Atomic primitives for Python. 7 | 8 | Links 9 | ````` 10 | * `documentation `_ 11 | * `development version `_ 12 | ''' 13 | 14 | import sys 15 | 16 | from setuptools import setup, find_packages 17 | from setuptools.command.test import test as TestCommand 18 | 19 | 20 | about = {} 21 | with open('atomos/__about__.py') as f: 22 | exec(f.read(), about) 23 | 24 | setup_requires = ['pytest', 'tox'] 25 | install_requires = ['six', 'tox'] 26 | tests_require = ['pytest-cov', 'pytest-cache', 'pytest-timeout'] 27 | dev_requires = ['pyflakes', 'pep8', 'pylint', 'check-manifest', 28 | 'ipython', 'ipdb', 'sphinx'] 29 | dev_requires.append(tests_require) 30 | 31 | 32 | class PyTest(TestCommand): 33 | def finalize_options(self): 34 | TestCommand.finalize_options(self) 35 | self.test_args = ['--strict', '--verbose', '--tb=long', 36 | '--cov', 'atomos', '--cov-report', 37 | 'term-missing', 'tests'] 38 | self.test_suite = True 39 | 40 | def run_tests(self): 41 | import pytest 42 | errno = pytest.main(self.test_args) 43 | sys.exit(errno) 44 | 45 | setup(name=about['__title__'], 46 | version=about['__version__'], 47 | author='Max Countryman', 48 | author_email=about['__author__'], 49 | description='Atomic primitives for Python.', 50 | license=about['__license__'], 51 | keywords='atom atomic concurrency lock', 52 | url='https://github.com/maxcountryman/atomos', 53 | packages=find_packages(exclude=['docs', 'tests']), 54 | long_description=__doc__, 55 | classifiers=['Development Status :: 5 - Production/Stable', 56 | 'Intended Audience :: Developers', 57 | 'Programming Language :: Python', 58 | 'Programming Language :: Python :: 2', 59 | 'Programming Language :: Python :: 2.7', 60 | 'Programming Language :: Python :: 3', 61 | 'Programming Language :: Python :: 3.3', 62 | 'Programming Language :: Python :: 3.4', 63 | 'Topic :: Utilities', 64 | 'License :: OSI Approved :: BSD License'], 65 | cmdclass={'test': PyTest}, 66 | setup_requires=setup_requires, 67 | install_requires=install_requires, 68 | tests_require=tests_require, 69 | extras_require={ 70 | 'dev': dev_requires, 71 | 'test': tests_require, 72 | }, 73 | zip_safe=False) 74 | -------------------------------------------------------------------------------- /tests/test_aref.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | tests.test_aref 4 | ''' 5 | import six 6 | import pytest 7 | 8 | import atomos.atom 9 | 10 | 11 | @pytest.fixture 12 | def aref(): 13 | return atomos.atom.ARef() 14 | 15 | 16 | def test_aref_get_watches(aref): 17 | assert aref.get_watches() == {} 18 | 19 | 20 | def test_aref_add_watch(aref): 21 | def watch(k, ref, old, new): 22 | pass 23 | 24 | aref.add_watch('foo', watch) 25 | 26 | assert 'foo' in aref.get_watches() 27 | assert watch in six.itervalues(aref.get_watches()) 28 | 29 | 30 | def test_aref_remove_watch(aref): 31 | def watch(k, ref, old, new): 32 | pass 33 | 34 | aref.add_watch('foo', watch) 35 | aref.remove_watch('foo') 36 | 37 | assert 'foo' not in aref.get_watches() 38 | assert watch not in six.itervalues(aref.get_watches()) 39 | 40 | 41 | def test_aref_notify_watches(aref): 42 | watches = ['foo', 'bar', 'baz'] 43 | watched = {} 44 | 45 | def watch(k, ref, old, new): 46 | watched[k] = {'old': old, 'new': new, 'ref': ref} 47 | 48 | for k in watches: 49 | aref.add_watch(k, watch) 50 | 51 | old, new = 'a', 'b' 52 | aref.notify_watches(old, new) 53 | 54 | for k in watches: 55 | assert k in watched 56 | assert watched[k] == {'old': old, 'new': new, 'ref': aref} 57 | -------------------------------------------------------------------------------- /tests/test_atom.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | tests.test_atom 4 | ''' 5 | 6 | import multiprocessing 7 | import threading 8 | 9 | import pytest 10 | 11 | import atomos.atom 12 | import atomos.multiprocessing.atom 13 | 14 | 15 | atoms = [(atomos.atom.Atom({}), threading.Thread), 16 | (atomos.multiprocessing.atom.Atom({}), multiprocessing.Process)] 17 | 18 | 19 | @pytest.fixture(params=atoms) 20 | def atom(request): 21 | return request.param 22 | 23 | 24 | def test_atom_deref(atom): 25 | atom, _ = atom 26 | assert atom.deref() == {} 27 | 28 | 29 | def test_atom_swap(atom): 30 | atom, _ = atom 31 | def update_state(cur_state, k, v): 32 | cur_state = cur_state.copy() 33 | cur_state[k] = v 34 | return cur_state 35 | 36 | atom.swap(update_state, 'foo', 'bar') 37 | assert atom.deref() == {'foo': 'bar'} 38 | 39 | 40 | def test_atom_reset(atom): 41 | atom, _ = atom 42 | assert atom.reset('foo') == 'foo' 43 | assert atom.deref() == 'foo' 44 | 45 | 46 | def test_atom_compare_and_set(atom): 47 | atom, _ = atom 48 | atom.reset('foo') 49 | 50 | assert atom.compare_and_set('foo', 'bar') is True 51 | assert atom.compare_and_set('foo', 'bar') is False 52 | 53 | 54 | def test_concurrent_swap(atom, proc_count=10, loop_count=1000): 55 | atom, proc = atom 56 | atom.reset(0) 57 | 58 | def inc_for_loop_count(): 59 | for _ in range(loop_count): 60 | atom.swap(lambda n: n + 1) 61 | 62 | processes = [] 63 | for _ in range(proc_count): 64 | p = proc(target=inc_for_loop_count) 65 | processes.append(p) 66 | p.start() 67 | 68 | for p in processes: 69 | p.join() 70 | 71 | assert atom.deref() == proc_count * loop_count 72 | 73 | 74 | def test_concurrent_compare_and_set(atom, proc_count=10, loop_count=1000): 75 | atom, proc = atom 76 | atom.reset(0) 77 | 78 | successes = multiprocessing.Value('i', 0) 79 | def attempt_inc_for_loop_count(successes): 80 | for _ in range(loop_count): 81 | oldval = atom.deref() 82 | newval = oldval + 1 83 | if atom.compare_and_set(oldval, newval): 84 | with successes.get_lock(): 85 | successes.value += 1 86 | 87 | processes = [] 88 | for _ in range(proc_count): 89 | p = proc(target=attempt_inc_for_loop_count, args=(successes,)) 90 | processes.append(p) 91 | p.start() 92 | 93 | for p in processes: 94 | p.join() 95 | 96 | assert atom.deref() == successes.value 97 | -------------------------------------------------------------------------------- /tests/test_atomic.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | tests.test_atomic 4 | ''' 5 | 6 | import multiprocessing 7 | import threading 8 | import ctypes 9 | 10 | import pytest 11 | 12 | import atomos.atomic 13 | import atomos.multiprocessing.atomic 14 | 15 | 16 | refs = [(atomos.atomic.AtomicReference({}), threading.Thread), 17 | (atomos.multiprocessing.atomic.AtomicReference({}), 18 | multiprocessing.Process)] 19 | 20 | 21 | class TestNumberT(atomos.atomic.AtomicNumber): 22 | def __init__(self, value=0): 23 | super(TestNumberT, self).__init__() 24 | self._value = value 25 | 26 | 27 | class TestNumberP(atomos.multiprocessing.atomic.AtomicNumber): 28 | def __init__(self, value=0): 29 | super(TestNumberP, self).__init__(typecode_or_type=ctypes.c_int, 30 | value=value) 31 | self._value = value 32 | 33 | 34 | numbers = [(TestNumberT(), threading.Thread), 35 | (TestNumberP(), multiprocessing.Process)] 36 | 37 | 38 | ints = [(atomos.atomic.AtomicInteger(), threading.Thread), 39 | (atomos.multiprocessing.atomic.AtomicInteger(), 40 | multiprocessing.Process)] 41 | 42 | 43 | @pytest.fixture(params=refs) 44 | def atomic_reference(request): 45 | atomic_ref, _ = request.param 46 | atomic_ref.set({}) 47 | return request.param 48 | 49 | 50 | @pytest.fixture(params=numbers) 51 | def atomic_number(request): 52 | atomic_number, _ = request.param 53 | atomic_number.set(0) 54 | return request.param 55 | 56 | 57 | @pytest.fixture(params=ints) 58 | def atomic_int(request): 59 | atomic_int, _ = request.param 60 | atomic_int.set(0) 61 | return request.param 62 | 63 | 64 | def test_atomic_reference_get(atomic_reference): 65 | atomic_reference, _ = atomic_reference 66 | assert atomic_reference.get() == {} 67 | 68 | 69 | def test_atomic_reference_set(atomic_reference): 70 | atomic_reference, _ = atomic_reference 71 | atomic_reference.set({'foo': 'bar'}) 72 | assert atomic_reference.get() == {'foo': 'bar'} 73 | 74 | 75 | def test_atomic_reference_get_and_set(atomic_reference): 76 | atomic_reference, _ = atomic_reference 77 | ret = atomic_reference.get_and_set({'foo': 'bar'}) 78 | assert atomic_reference.get() == {'foo': 'bar'} 79 | assert ret == {} 80 | 81 | 82 | def test_atomic_reference_compare_and_set(atomic_reference): 83 | atomic_reference, _ = atomic_reference 84 | assert atomic_reference.compare_and_set({}, {'foo': 'bar'}) is True 85 | assert atomic_reference.compare_and_set({}, {'foo', 'bar'}) is False 86 | 87 | 88 | def test_atomic_number_add_and_get(atomic_number): 89 | atomic_number, _ = atomic_number 90 | assert atomic_number.add_and_get(1) == 1 91 | 92 | 93 | def test_atomic_number_get_and_add(atomic_number): 94 | atomic_number, _ = atomic_number 95 | assert atomic_number.get_and_add(1) == 0 96 | 97 | 98 | def test_atomic_number_subtract_and_get(atomic_number): 99 | atomic_number, _ = atomic_number 100 | assert atomic_number.subtract_and_get(1) == -1 101 | 102 | 103 | def test_atomic_number_get_and_subtract(atomic_number): 104 | atomic_number, _ = atomic_number 105 | assert atomic_number.get_and_subtract(1) == 0 106 | 107 | 108 | def test_concurrent_atomic_int(atomic_int, proc_count=10, loop_count=1000): 109 | atomic_int, proc = atomic_int 110 | 111 | def inc_atomic_int(): 112 | for _ in range(loop_count): 113 | atomic_int.add_and_get(1) 114 | 115 | processes = [] 116 | for _ in range(proc_count): 117 | p = proc(target=inc_atomic_int) 118 | processes.append(p) 119 | p.start() 120 | 121 | for p in processes: 122 | p.join() 123 | 124 | assert atomic_int.get() == proc_count * loop_count 125 | 126 | 127 | def test_concurrent_atomic_ref(atomic_reference, 128 | proc_count=10, 129 | loop_count=1000): 130 | atomic_reference, proc = atomic_reference 131 | atomic_reference.set({'count': 0}) 132 | assert atomic_reference.get() == {'count': 0} 133 | 134 | def update(d): 135 | # N.B. A mutable object should be copied such that the update function 136 | # does not change the original object in memory. Failure to do so will 137 | # cause unexpected results! 138 | d = d.copy() 139 | d['count'] += 1 140 | return d 141 | 142 | def inc_atomic_ref(): 143 | for _ in range(loop_count): 144 | while True: 145 | oldval = atomic_reference.get() 146 | newval = update(oldval) 147 | is_set = atomic_reference.compare_and_set(oldval, newval) 148 | if is_set: 149 | break 150 | 151 | processes = [] 152 | for _ in range(proc_count): 153 | p = proc(target=inc_atomic_ref) 154 | processes.append(p) 155 | p.start() 156 | 157 | for p in processes: 158 | p.join() 159 | 160 | assert atomic_reference.get()['count'] == proc_count * loop_count 161 | -------------------------------------------------------------------------------- /tests/test_util.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | ''' 3 | tests.test_util 4 | ''' 5 | 6 | import threading 7 | import multiprocessing 8 | 9 | import atomos.util 10 | 11 | 12 | def test_synchronized(thread_count=10, loop_count=1000): 13 | class SharedInt(object): 14 | value = 0 15 | 16 | shared_int = SharedInt() 17 | 18 | def inc(n): 19 | return n + 1 20 | 21 | @atomos.util.synchronized 22 | def inc_shared_int(): 23 | for _ in range(loop_count): 24 | shared_int.value = inc(shared_int.value) 25 | 26 | threads = [] 27 | for _ in range(thread_count): 28 | t = threading.Thread(target=inc_shared_int) 29 | threads.append(t) 30 | t.start() 31 | 32 | for t in threads: 33 | t.join() 34 | 35 | assert shared_int.value == thread_count * loop_count 36 | 37 | 38 | def test_readers_writer_lock(acquire_shared_count=10): 39 | lock = atomos.util.ReadersWriterLock() 40 | 41 | for _ in range(acquire_shared_count): 42 | lock.shared.acquire() 43 | 44 | assert lock._reader_count == acquire_shared_count 45 | 46 | # Cannot acquire an exclusive lock with active readers. 47 | t = threading.Thread(target=lock.exclusive.acquire) 48 | t.start() 49 | 50 | t.join(1.0) 51 | 52 | assert t.is_alive() is True 53 | 54 | for _ in range(acquire_shared_count): 55 | lock.shared.release() 56 | 57 | assert lock._reader_count == 0 58 | 59 | t.join() 60 | 61 | assert t.is_alive() is False 62 | 63 | # Cannot acquire an exclusive lock with an active writer. (This was 64 | # acquired in the above thread. 65 | t = threading.Thread(target=lock.exclusive.acquire) 66 | t.start() 67 | 68 | t.join(1.0) 69 | 70 | assert t.is_alive() is True 71 | 72 | # Release the original acquisition. 73 | lock.exclusive.release() 74 | 75 | t.join() 76 | 77 | assert t.is_alive() is False 78 | 79 | # Cannot acquire a shared lock with an active writer. (This was acquired 80 | # in the above thread.) 81 | t = threading.Thread(target=lock.shared.acquire) 82 | t.start() 83 | 84 | t.join(1.0) 85 | 86 | assert t.is_alive() is True 87 | 88 | lock.exclusive.release() 89 | 90 | t.join() 91 | 92 | assert t.is_alive() is False 93 | assert lock._reader_count == 1 94 | 95 | 96 | def test_readers_writer_lock_multiprocessing(acquire_shared_count=10): 97 | lock = atomos.util.ReadersWriterLockMultiprocessing() 98 | 99 | for _ in range(acquire_shared_count): 100 | lock.shared.acquire() 101 | 102 | assert lock._reader_count.value == acquire_shared_count 103 | 104 | # Cannot acquire an exclusive lock with active readers. 105 | p = multiprocessing.Process(target=lock.exclusive.acquire) 106 | p.start() 107 | 108 | p.join(1.0) 109 | 110 | assert p.is_alive() is True 111 | 112 | for _ in range(acquire_shared_count): 113 | lock.shared.release() 114 | 115 | assert lock._reader_count.value == 0 116 | 117 | p.join() 118 | 119 | assert p.is_alive() is False 120 | 121 | # Cannot acquire an exclusive lock with an active writer. (This was 122 | # acquired in the above process. 123 | p = multiprocessing.Process(target=lock.exclusive.acquire) 124 | p.start() 125 | 126 | p.join(1.0) 127 | 128 | assert p.is_alive() is True 129 | 130 | # Release the original acquisition. 131 | lock.exclusive.release() 132 | 133 | p.join() 134 | 135 | assert p.is_alive() is False 136 | 137 | # Cannot acquire a shared lock with an active writer. (This was acquired 138 | # in the above process.) 139 | p = multiprocessing.Process(target=lock.shared.acquire) 140 | p.start() 141 | 142 | p.join(1.0) 143 | 144 | assert p.is_alive() is True 145 | 146 | lock.exclusive.release() 147 | 148 | p.join() 149 | 150 | assert p.is_alive() is False 151 | assert lock._reader_count.value == 1 152 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py27,py33,py34,pypy,docs 3 | 4 | [testenv] 5 | commands = python setup.py test 6 | 7 | [testenv:py27] 8 | basepython = python2.7 9 | 10 | [testenv:py33] 11 | basepython = python3.3 12 | 13 | [testenv:py34] 14 | basepython = python3.4 15 | 16 | [testenv:pypy] 17 | basepython = pypy 18 | 19 | [testenv:docs] 20 | changedir=docs 21 | commands= 22 | /usr/bin/make clean html 23 | --------------------------------------------------------------------------------