sense core
Project description
sense-core
sense-core目前包含的功能主要有:
1)配置解析和管理
2)日志打印和收集
3)mysql、es、redis、rabbit基类
4)唯一主键生成
5)多线程和多进程操作封装
6)实用函数
安装方式
pip install sense-core
当前版本是0.1.12
使用指南
使用
import sense_core as sd
导入模块。sense_core实现上是把库里的文件都导入到__init__.py,所以不需要指定sense_core下的文件。 另外,最好使用sd别名,避免和其他库使用上冲突。
配置解析和管理
约定:项目根目录放置配置文件settings.ini,按模块label分块配置各服务模块,格式类似:
[rabbit]
host = 52.82.48.248
port = 5672
user = admin
password = sense_mq@2018
通用配置(如log_path)放到[settings]下,[settings]建议放到配置文件最后。
程序内通过sd.config('label','item')调用,要确保item的key是存在的,否则解析配置会抛出异常。
如果不确定item是否存在可以使用sd.config('label','item',''),不存在的item会赋默认的空值。
对于非docker部署模式,根目录可以放settings.local.ini用于本地开发使用,该文件不要提到git里。
日志打印和收集
Mysql封装
ES封装
RabbitMQ封装
Reids 封装
只是简单的提供了一个 get_redis_client
其他实用方法
装饰器 try_catch_exception
脚本的最顶层调用最好都加上这个装饰器,把抛出的异常都捕捉到
装饰器 catch_raise_exception
底层代码调用,打出log并把日志抛到外部
另外,django项目可以依赖sense-django,其提供了 @catch_view_exception ,在每个view方法上面装饰下, 不需要自己catch异常返回错误的json的信息,简化代码。
多线程和多进程封装
示例代码:
def handle_process_work(job):
sd.log_info("job={0}".format(job))
time.sleep(0.1)
def test_multi_process():
jobs = [i for i in range(100)]
sd.execute_multi_core("dumb", handle_process_work, jobs, 4, True)
execute_multi_core的最后一个参数表示使用多线程还是多进程,如果work num是1就主进程顺序执行了。
其他
可参考utils.py源码
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sense-core-0.2.19.tar.gz.
File metadata
- Download URL: sense-core-0.2.19.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
706610dec81f47e8cfb647b40bccc6f942f831533a7b01b399844a1ddf083208
|
|
| MD5 |
a46afa08946b114eb3cb1c5aeeadf138
|
|
| BLAKE2b-256 |
83b89c3614579a95ee3eebcd8fd75c404770207a2fa804b7f2de0f66d76fcd8e
|
File details
Details for the file sense_core-0.2.19-py3-none-any.whl.
File metadata
- Download URL: sense_core-0.2.19-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d1de68852d4f534f99e1bef6557e2b82ec0851b8a079fedd2b3819f03b7cd2c
|
|
| MD5 |
767ec98db74a51f9e5b07d7ae8d1f118
|
|
| BLAKE2b-256 |
c31ea03d0a9f7f1d367d1b567433c8397da6d57b4cbfed515736cbecbad8e977
|