A lazy connection context manager
Project description
A lazy connection context manager
使用说明
LazyConnection 是一个线程安全的连接管理器,它将会创建一个连接上下文(context),这个上下文管理一堆在它上面注册的工场函数(factory)。
在context 的生命周期范围内,factory 将会根据使用到的情况按需创建(最多只会被创建一次), 通过以 context 上注册的名字作为属性来访问, 可得到该 factory 的一个实例。
在context 的生命周期结束时,将会自动执行清理工作,factory 的实例会被销毁,如果有 close 方法也将被调用。
使用范例:
from lazyconn import LazyConnection
def create_test1_client():
return snow(host='127.0.0.1', port=1234)
def create_test2_client1():
return wish(host='127.0.0.1', port=1235)
# register
LazyConnection.register_factory('test', create_test1_client)
LazyConnection.register_factory('test2', create_test2_client)
# context life
with LazyConnection() as conn:
conn.test1.hello('wayhome')
lg 是一个线程安全的全局对象,它只在连接上下文中有效,它的 conn 属性对应一个连接上下文。
使用范例:
from lazyconn.globals import lg
with LazyConnection() as conn:
conn.test1.hello('wayhome')
lg.conn.test1.hello('wayhome2')
thread_safe 是一个装饰器, 被装饰的函数将会在一个连接上下文中执行。lg 对象可以在被装饰的函数内部使用。
使用范例:
from lazyconn import thread_safe
@thread_safe
def test():
lg.conn.test1.hello('wayhome3')
History
0.1.0 (2013-08-11)
First release on PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
lazyconn-0.1.0.tar.gz
(7.8 kB
view details)
File details
Details for the file lazyconn-0.1.0.tar.gz.
File metadata
- Download URL: lazyconn-0.1.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d1665f98ec0e9d6132655ea576707c4afbc3d11caa6e67ec6b56be55f96b190
|
|
| MD5 |
00dd333ce745586e5b52b9f7c048ee5e
|
|
| BLAKE2b-256 |
79a088d7bd1c9f6beaa70a52a3752b93f4eb596ef1df0143e11b7f11aa5a299a
|