::: tip 注意:
- 请使用 Python 3.7+ 版本进行开发
- 除了特别需求外,一般情况下,Linux ,Mac ,Win 代码是一致的
:::
本文列子
- https://github.com/w5teams/Demo
创建项目格式
``` ├── helloword # APP 目录名 │ ├── app.json # APP 配置文件 │ ├── icon.png # APP 图标 │ ├── linux # APP Linux 版本 │ │ └── run.py # APP Linux 入口文件 │ ├── mac # APP Mac 版本 │ │ └── run.py # APP Mac 入口文件 │ ├── windows # APP Win 版本 | | └── run.py # APP Win 入口文件 │ └── readme.md # APP 说明文件
## 编写代码**Linux** ,**Mac** ,**Win** 3个目录下的 `run.py` 保持一致
!/usr/bin/env python
encoding:utf-8
from loguru import logger # 导入日记库,没有请先安装 pip install loguru
为了保证性能,使用了 async await 开启异步携程,Python 3.7+ 的特性
async def hello_word(name):
# 输出日记,生产环境会输出到指定目录logger.info("[Hello Word] 该 APP 执行参数为: {name}", name=name)# 返回值,格式为: 状态码,返回内容return {"status": 0, "result": "Hello," + name}
**async** , **await** 使用 Demo
!/usr/bin/env python
encoding:utf-8
from loguru import logger
async def hello_word(name): try: import requests except: logger.info(“[Hello Word] 导入 requests 模块失败, 请输入命令 pip install requests”) return 2, “缺少 requests 模块”
# 使用 awaitr = await requests.get(url="https://w5.io")print(r)logger.info("[Hello Word] 该 APP 执行参数为: {name}", name=name)return {"status":0,"result":"Hello," + name,"html": '''<span style="color:red">{name}</span>'''.format(name="Hello," + name)}
## APP 测试使用 **asyncio** 运行测试,正式发布后请删除 **main** 入口函数
!/usr/bin/env python
encoding:utf-8
from loguru import logger # 导入日记库,没有请先安装 pip install loguru
为了保证性能,使用了 async await 开启异步,Python 3.7+ 的特性
async def hello_word(name):
# 输出日记,生产环境会输出到指定目录logger.info("[Hello Word] 该 APP 执行参数为: {name}", name=name)# 返回值,格式为: 状态码,返回内容return {"status": 0, "result": "Hello," + name}
if name == ‘main‘:
# 导入异步库import asyncio# 测试函数async def test():result = await hello_word("W5")print(result)# 加入异步队列async def main(): await asyncio.gather(test())# 启动执行asyncio.run(main())
```
执行演示

