2019独角兽企业重金招聘Python工程师标准>>>

什么是stevedore?

stevedore是建立在setuptools的entry point的功能上的,用于python程序动态加载代码,在openstack中被多个组件使用:比如ceilometer,neutron的plugin。当然,你可以直接使用

python的某些黑魔法实现插件的加载,但太原始了。stevedore基于entry point提供了更高层次的封装。

stevedore的官方文档在此:http://docs.openstack.org/developer/stevedore/

学习和入门setuptools:http://www.360doc.com/content/14/0306/11/13084517_358166737.shtml

官方文档的部分翻译:http://www.360doc.com/content/14/0429/19/9482_373285413.shtml

来自华为孔令贤(源地址非该人博客)的setup.py详解:http://blog.sina.com.cn/s/blog_4951301d0101etvj.html

偶计划在sora项目中引入stevedore与oslo.config简化某些开发的组件,先是测试了stevedore,写了个简单的scheduler插件

环境准备:

安装stevedore库,及组织相关目录

pip install stevedore
mkdir sora
cd sora
mkdir scheduler  #scheduler在sora目录中

构建这样一个目录树:

步骤:

创建一个抽象类scheduler,新的plugin要继承scheduler并重写相关方法scheduler

#sora/scheduler/base.py
import abcclass scheduler(object):__metaclass__ = abc.ABCMeta@abc.abstractmethoddef scheduler(self,data):pass

继承base类创建插件simple与memory

#sora/scheduler/memory.py
import base
class memoryscheduler(base.scheduler):def scheduler(self,data):id = data[max(data['memory'])]return id
#sora/scheduler/simple.py
import base
import random
class simplescheduler(base.scheduler):def scheduler(self,data):id = data[random.choice(data['memory'])]return id

simple插件是随机选择一个节点id,而memory则选择内存剩余最多的节点id

编写setup.py

#sora/setup.py
from setuptools import setup, find_packages
setup(name='sora-scheduler',version='1.0',description='sora.scheduler',author='hochikong',author_email='hochikong',platforms=['Any'],scripts=[],# provides=['sora.scheduler',#           ],packages=find_packages(),include_package_data=True,entry_points={'sora.scheduler': ['memorybase = scheduler.memory:memoryscheduler','randombase = scheduler.simple:simplescheduler',],},zip_safe=False,
)

安装自己编写的包:

root@workgroup1:~/sora# python setup.py install
running install
running bdist_egg
running egg_info
creating sora_scheduler.egg-info
writing sora_scheduler.egg-info/PKG-INFO
writing top-level names to sora_scheduler.egg-info/top_level.txt
writing dependency_links to sora_scheduler.egg-info/dependency_links.txt
writing entry points to sora_scheduler.egg-info/entry_points.txt
writing manifest file 'sora_scheduler.egg-info/SOURCES.txt'
reading manifest file 'sora_scheduler.egg-info/SOURCES.txt'
writing manifest file 'sora_scheduler.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/scheduler
copying scheduler/__init__.py -> build/lib.linux-x86_64-2.7/scheduler
copying scheduler/base.py -> build/lib.linux-x86_64-2.7/scheduler
copying scheduler/memory.py -> build/lib.linux-x86_64-2.7/scheduler
copying scheduler/simple.py -> build/lib.linux-x86_64-2.7/scheduler
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/scheduler
copying build/lib.linux-x86_64-2.7/scheduler/__init__.py -> build/bdist.linux-x86_64/egg/scheduler
copying build/lib.linux-x86_64-2.7/scheduler/base.py -> build/bdist.linux-x86_64/egg/scheduler
copying build/lib.linux-x86_64-2.7/scheduler/memory.py -> build/bdist.linux-x86_64/egg/scheduler
copying build/lib.linux-x86_64-2.7/scheduler/simple.py -> build/bdist.linux-x86_64/egg/scheduler
byte-compiling build/bdist.linux-x86_64/egg/scheduler/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/scheduler/base.py to base.pyc
byte-compiling build/bdist.linux-x86_64/egg/scheduler/memory.py to memory.pyc
byte-compiling build/bdist.linux-x86_64/egg/scheduler/simple.py to simple.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying sora_scheduler.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying sora_scheduler.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying sora_scheduler.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying sora_scheduler.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying sora_scheduler.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying sora_scheduler.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
creating dist
creating 'dist/sora_scheduler-1.0-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing sora_scheduler-1.0-py2.7.egg
creating /usr/local/lib/python2.7/dist-packages/sora_scheduler-1.0-py2.7.egg
Extracting sora_scheduler-1.0-py2.7.egg to /usr/local/lib/python2.7/dist-packages
Adding sora-scheduler 1.0 to easy-install.pth fileInstalled /usr/local/lib/python2.7/dist-packages/sora_scheduler-1.0-py2.7.egg
Processing dependencies for sora-scheduler==1.0
Finished processing dependencies for sora-scheduler==1.0

尝试手动加载插件:

root@workgroup1:~/sora# python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from scheduler.memory import memoryscheduler
>>> dt = {13:'id1',324:'id2',434:'id3','memory':[13,324,434]}
>>> driver = memoryscheduler()
>>> driver.scheduler(dt)
'id3'
>>>

不过手动加载并没有太大意义

使用stevedore的drivermanager:

>>> from stevedore import driver
>>> dt = {13:'id1',324:'id2',434:'id3','memory':[13,324,434]}
>>> mgr = driver.DriverManager(
...     namespace='sora.scheduler',
...     name='randombase',
...     invoke_on_load=True,         #设置为true,即载入后自动实例化插件类,如果是函数,则调用
... )
>>> mgr.driver.scheduler(dt)
'id3'
>>> mgr.driver.scheduler(dt)
'id2'
>>> mgr.driver.scheduler(dt)
'id3'
>>> mgr.driver.scheduler(dt)
'id2'
>>> mgr.driver.scheduler(dt)
'id1'
>>>

这里我导入了randombase,怎样调用plugin里的方法很明显了

顺带检查下python中我的包的安装状况:

root@workgroup1:~# cd /usr/local/lib/python2.7/dist-packages/
root@workgroup1:/usr/local/lib/python2.7/dist-packages# ls
amqp                                           OpenSSL
amqp-1.4.6.dist-info                           pbr
anyjson                                        pbr-1.3.0.dist-info
anyjson-0.3.3.egg-info                         pika
backports                                      pika-0.9.14.egg-info
backports.ssl_match_hostname-3.4.0.2.egg-info  psutil
billiard                                       psutil-2.2.1.egg-info
billiard-3.3.0.19.egg-info                     _psutil_linux.so
_billiard.so                                   _psutil_posix.so
bottle-0.12.8.egg-info                         pymongo
bottle.py                                      pymongo-2.8.egg-info
bottle.pyc                                     pyOpenSSL-0.15.1.dist-info
bson                                           python_etcd-0.3.3.egg-info
celery                                         pytz
celery-3.1.17.dist-info                        pytz-2015.2.dist-info
docker                                         six-1.9.0.dist-info
docker_py-1.0.0.egg-info                       six.py
easy-install.pth                               six.pyc
etcd                                           sora_scheduler-1.0-py2.7.egg
eventlet                                       SQLAlchemy-0.9.9-py2.7-linux-x86_64.egg
eventlet-0.17.1.dist-info                      stevedore
funtests                                       stevedore-1.6.0.dist-info
glances                                        tests
Glances-2.3.egg-info                           virtualenv-12.0.7.dist-info
greenlet-0.4.5.egg-info                        virtualenv.py
greenlet.so                                    virtualenv.pyc
gridfs                                         virtualenv_support
kombu                                          websocket
kombu-3.0.24.dist-info                         websocket_client-0.25.0.egg-info

可以看到里面有个sora_scheduler-1.0-py2.7.egg目录,查看一下:

我不太清楚为什么python在导入scheduler模块时,并不需要指明sora_scheduler-1.0-py2.7.egg,可能与egg有关,执行import scheduler时,导入的是sora_scheduler-1.0-py2.7.egg目录下

的scheduler包,同样的还有SQLAlchemy,有一个SQLAlchemy-0.9.9-py2.7-linux-x86_64.egg,导入时只需执行import sqlalchemy

疑问:

可能有人会问,如果我想添加新的插件要怎么办,我想,最好的方法就是修改setup.py,更新安装一次该包即可

参考:

http://docs.openstack.org/developer/stevedore/

http://www.360doc.com/content/14/0306/11/13084517_358166737.shtml

http://www.360doc.com/content/14/0429/19/9482_373285413.shtml

转载于:https://my.oschina.net/hochikong/blog/477665

#Sora#openstack基础库stevedore试用总结相关推荐

  1. #Sora#OpenStack基础库oslo.config试用总结

    2019独角兽企业重金招聘Python工程师标准>>> 什么是oslo.config? oslo.config是OpenStack用于解析配置文件和命令行参数的工具,大概是封装了ar ...

  2. 更改微信小程序的基础版本库;更改uni-app小程序基础库;更改用户的微信小程序基础库最低版本;设置用户的微信小程序版本库;

    需求场景:微信小程序不少API都有最低版本支持,为了避免不必要的麻烦,我们可以根据需要给小程序设置基础库最低版本,这样若用户使用的基础库版本低于设置的最低版本要求,则无法正常使用小程序,并提示更新微信 ...

  3. OpenStack 基础知识

    OpenStack 基础知识 文章目录 OpenStack 基础知识 前言 一.虚拟化 1.I型虚拟化 2.II型虚拟化 二.KVM & Libvirt 前言 了解openstack之前需要掌 ...

  4. 云计算之OpenStack基础

    云计算之OpenStack基础 一.OpenStack基础知识 二.虚拟化 2.1 虚拟化类型 2.1.1 Ⅰ型虚拟化 2.1.2 Ⅱ型虚拟化 2.1.3 比较 2.2 KVM(Ⅱ型虚拟化) 2.2. ...

  5. python 基础命令-Python 命令行(CLI)基础库

    在 CLI 下写 UI 应用 前阵子看了一下自己去年写的 Python-视频转字符动画,感觉好糗..所以几乎把整篇文章重写了一遍.并使用 curses 库实现字符动画的播放. 但是感觉,curses ...

  6. python的openpyxl库如何读取特定列_Excelize 2.3.2 发布,Go 语言 Excel 文档基础库,2021 年首个更新...

    Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准.可以使用它来读取.写入由 Microsoft Exc ...

  7. go读取excel_Excelize 2.3.0 发布,Go 语言 Excel 文档基础库

    github.com/xuri/excelize Excelize 是 Go 语言编写的用于操作 Office Excel 文档基础库,基于 ECMA-376,ISO/IEC 29500 国际标准.可 ...

  8. go基础库之环境变量的获取与设置以及如何使用默认值

    golang 基础库之 go 获取命令行参数,介绍了环境变量的获取与设置以及如何使用默认值 环境变量的获取与设置以及如何使用默认值 Golang 版本 1.12.1 前言 环境变量作为配置选项可以显著 ...

  9. 【转】《从入门到精通云服务器》第六讲—OpenStack基础

    前五期的<从入门到精通云服务器>受到了广泛好评,收到留言,有很多读者对云计算相关的技术非常感兴趣.应观众要求,我们这期要安利一条纯技术内容.准备好瓜子.花生,随小编一起进入OpenStac ...

最新文章

  1. CVPR2021 | 视觉 Transformer 的可视化
  2. Sublime text 2/3 中 Package Control 的安装与使用方法
  3. python运输问题_叶片运输优化问题学习笔记
  4. c3p0依赖导入失败问题
  5. Thread.Join()方法的理解
  6. redis配置文件conf详解
  7. 控制工程基础Chapter2 Mathematical models of systems
  8. RTOS原理与实现04:任务管理模块
  9. 没了Macbook的英特尔还好吗?比你想象的好
  10. JSP的3种方式实现radio ,checkBox,select的默认选择值
  11. AJAX做一个动态进度条
  12. 安装完毕后VS2012(2013)中找不到ADO.NET Entity Data Model模板或 sql server database project模板
  13. win7怎么修改计算机皮肤,鼠标指针怎么换?小编教你win7系统更换鼠标指针皮肤的方法...
  14. 科普任重而道远:生物信息为什么要学 Linux?
  15. 【iOS-Cocos2d游戏开发之十六】添加本地通知(UILocalNotification)以及添加系统组件滚动视图(UIScrollView)!【2011年11月15日更新】
  16. cocos Creator android 上传图片与数据
  17. python输出偶数_python程序使用递归查找数字是偶数还是奇数
  18. None、Null与空字符‘‘什么区别
  19. 英语cymophanite猫眼石cymophanite单词
  20. 怎么用html写一个学校代码,[代码学校]简单实用的HTML代码分享_格桑梅朵_天涯部落_天涯社区...

热门文章

  1. 如何判断函数凸或非凸?
  2. Google系列②布局平台战略
  3. 用计算机玩穿越火线,为什么每次玩穿越火线电脑就特别卡?
  4. vc6.0静态链接库的创建与使用方法
  5. 计算机软件的英文翻译,软件的英文翻译
  6. 查找python包路径sys_2.2 最快查看包搜索路径的方式
  7. 汽车车漆计算机调色的基本程序,汽车喷漆调色有什么程序标准
  8. 游戏引擎中的实时渲染和在V-Ray中渲染有什么区别
  9. java中map嵌套map_java中遍历MAP,嵌套map的几种方法
  10. numpy的常用函数