satella.coding.resources package¶
Submodules¶
satella.coding.resources.cp_manager module¶
- class satella.coding.resources.cp_manager.CPManager(max_number, max_cycle_no)¶
Bases:
Monitor
,Closeable
,Generic
[T
]A thread-safe no-hassle connection-pool manager.
Extend this class to build your own connection pool managers.
This supports automatic connection recycling, connection will be cycled each max_cycle_no takings and deposits.
Note that you have to overload
teardown_connection()
andcreate_connection()
.You obtain a connection by using
acquire_connection()
. If it fails you should mark it as such usingfail_connection()
. In all cases you have to return it usingrelease_connection()
.- Parameters:
max_number (int) – maximum number of connections
max_cycle_no (int) – maximum number of get/put connection cycles.
- Variables:
max_number – maximum amount of connections. Can be changed during runtime
Warning
May not work under PyPy for reasons having to do with id’s semantics. A RuntimeWarning will be issued when not running under CPython.
You need to invoke this at your constructor You can also use it to release locks of other objects.
- acquire_connection()¶
Either acquire a new connection, or just establish it in the background
- Returns:
a new connection:
- Raises:
RuntimeError – CPManager is terminating!
- Return type:
T
- close()¶
Check if the resource needs cleanup, and clean up this resource.
Use like this:
>>> class MyClose(Closeable): >>> def close(self): >>> if super().close(): >>> .. clean up ..
- Returns:
whether the cleanup should proceed
- Raises:
RuntimeError – the constructor was not invoked
- Return type:
None
- abstract create_connection()¶
Create a new connection.
Is safe to block.
- Returns:
a new connection instance
- Return type:
T
- fail_connection(connection)¶
Signal that a given connection has been failed
- Parameters:
connection (T) – connection to fail
- Return type:
None
- invalidate()¶
Close all connections. Connections have to be released first. Object is ready for use after this
- Return type:
None
- release_connection(connection)¶
Release a connection
- Parameters:
connection (T) – connection to release
- Return type:
None
- abstract teardown_connection(connection)¶
Close the connection.
Is safe to block.
- Parameters:
connection (T) – connection to tear down
- Return type:
None