Eurasia/框架功能介绍
查看全部词条命名空间:Main
模板:Eurasia top 这里介绍一些有用的框架工具。
使用sleep暂停线索
core.sleep(sec) 是标准库 time.sleep() 在框架中的替代实现:
from eurasia.core import sleep ... sleep(0.5) # 程序暂停 0.5 秒 ...
创建协程
在 eurasia 中可以直接使用 greenlet 库创建协程。
from time import time
from eurasia.greenlet import greenlet
from eurasia.core import sleep, mainloop
def loop():
while 1:
print time()
sleep(1.)
greenlet(loop).switch()
mainloop()
调试
通过设置 core.excepthook 安装调试钩子。
# -*- coding: utf-8 -*-
import sys, traceback
from eurasia import core
from eurasia.web import httpserver, mainloop
# 安装调试钩子
def excepthook():
traceback.print_exc(file=sys.stderr)
core.excepthook = excepthook
def handler(httpfile):
print not_exists # 这里会报错,变量 not_exists 不存在
httpfile.close()
httpd = httpserver(('', 8080), handler)
httpd.start()
mainloop()
发布产品时,通过 core.without_excepthook() 调用忽略调试钩子。
# -*- coding: utf-8 -*-
import sys, traceback
from eurasia import core
from eurasia.web import httpserver, mainloop
# 安装调试钩子
def excepthook():
traceback.print_exc(file=sys.stderr)
core.excepthook = excepthook
...
core.without_excepthook() # 忽略前面设置的调试钩子
配置文件
eurasia.pyetc 模块为配置文件的读取提供了支持。eurasia 认为使用 python 语法来编写配置文件是个好主意。
pyetc 中的 load(filename, **env) 函数能读取指定的 python 源码文件(文件后缀并不需要一定是“.py”), 并设定配置文件中的可见环境 env。
load 函数能将指定源码文件转换成 python 模块并返回。
我们首先编写一个名为 httpd.conf 的配置文件:
Server(controller='Products.default.controller'
port=8080)
user = 'nobody'
下面是对于这个配置文件的解析:
from eurasia import pyetc
config = {}
def Server(**args):
config.update(args)
mod = pyetc.load('httpd.conf', env={Server:Server})
print 'Server:', config
print 'user', mod.user
pyetc 模块可以被用于导入执行任意指定路径和文件后缀的 python 模块