Package: deva
Thedevalib makes it easy to write streaming data process pipelines,event driven programing,and run async function.An example of a streaming process and web view# coding: utf-8fromdeva.pageimportpage,render_templatefromdevaimport*# 系统日志监控s=from_textfile('/var/log/system.log')s1=s.sliding_window(5).map(concat('<br>'),name='system.log日志监控')s.start()# 实时股票数据s2=timer(func=lambda:NB('sample')['df'].sample(5).to_html(),start=True,name='实时股票数据',interval=1)# 系统命令执行command_s=Stream.from_process(['ping','baidu.com'])s3=command_s.sliding_window(5).map(concat('<br>'),name='系统持续命令')command_s.start()s1.webview()s2.webview()s3.webview()Monitor().start()Deva.run()FeaturesLicenseCopyright spark, 2018-2020.Installpipinstalldevaorpip3installdevaSample<b>如果是在jupyter里执行带码，代码尾部不需要添加Deva.run()
</b>bus<b>如果使用bus跨进程，需要安装redis 5.0</b>fromdevaimport*# 每隔一秒写入秒数到bus中timer(start=True)>>bus# 打印来自bus到数据bus>>logDeva.run()fromdevaimport*# bus中的证书进行乘2后打印日志bus.filter(lambdax:isinstance(x,int)).map(lambdax:x*2)>>log# bus中来的原始数据全部打印报警bus>>warnDeva.run()Crawlerfromdevaimport*h=http()h.map(lambdar:(r.url,r.html.search('<title>{}</title>')[0]))>>log'http://www.518.is'>>hs=Stream()s.rate_limit(1).http(workers=20).map(lambdar:(r.url,r.html.search('<title>{}</title>')[0]))>>warn'http://www.518.is'>>sDeva.run()timerfromdevaimporttimer,log,Deva,warn# 默认每秒执行一次，返回当前秒timer(start=True)>>log# 3秒返回一个yahoo，随后启动，结果报警warns=timer(func=lambda:'yahoo',interval=3)s.start()s>>warn# 可用stop方法停止一个定时器# s.stop()Deva.run()# python3 每隔n秒执行.py# [2020-03-14 10:31:16.847544] INFO: log: 16# WARNING:root:yahoo# [2020-03-14 10:31:17.849576] INFO: log: 17# [2020-03-14 10:31:18.853488] INFO: log: 18# WARNING:root:yahoo# [2020-03-14 10:31:19.855116] INFO: log: 19# [2020-03-14 10:31:20.859602] INFO: log: 20# [2020-03-14 10:31:21.865973] INFO: log: 21# WARNING:root:yahoo# [2020-03-14 10:31:22.868624] INFO: log: 22schedulerfromdevaimport*s=Stream.scheduler()# 5秒执行一次的任务，返回yahoo到s中s.add_job(func=lambda:'yahoo',seconds=5)# 5秒执行一次的任务，发送yamaha到bus，且返回yamaha到s中s.add_job(func=lambda:'yamaha'>>bus,seconds=5)# 返回open到s中，每天执行一次，启动时间9点25s.add_job(name='open',func=lambda:'open',days=1,start_date='2019-04-03 09:25:00')# 发送关闭到bus，返回值close放到s中，每天执行一次，15点30开始执行deffoo():'关闭'>>busreturn'close's.add_job(name='close',func=foo,days=1,start_date='2019-04-03 15:30:00')# 打印所有任务s.get_jobs()|pmap(lambdax:x.next_run_time)|ls|print# 放入s中的所有数据都打印日志s>>logbus.map(lambdax:x*2)>>warnDeva.run()# $ python3 time_scheduler/scheduler.py# [datetime.datetime(2020, 3, 14, 18, 6, 17, 830399, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), datetime.datetime(2020, 3, 14, 18, 6, 17, 830947, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), datetime.datetime(2020, 3, 15, 9, 25, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>), datetime.datetime(2020, 3, 15, 15, 30, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)]# [2020-03-14 10:06:17.835725] INFO: log: yahoo# [2020-03-14 10:06:17.839594] INFO: log: yamaha# WARNING:root:yamahayamaha# [2020-03-14 10:06:22.846482] INFO: log: yahoo# [2020-03-14 10:06:22.851722] INFO: log: yamaha# WARNING:root:yamahayamaha# [2020-03-14 10:06:27.840823] INFO: log: yahoworkersfromdevaimportbus,log,when,Deva# 开盘任务@bus.route(lambdax:x=='open')defonopen(x):'open'>>log# 收盘任务@bus.route(lambdax:x=='close')defonclose(x):'close'>>log# 另外一种写法when('open',source=bus).then(lambda:print(f'开盘啦'))Deva.run()