::: tip 注意:

  • 请使用 Python 3.7+ 版本进行开发
  • 除了特别需求外,一般情况下,LinuxMacWin 代码是一致的

:::

本文列子

  • 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 说明文件
  1. ## 编写代码
  2. **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):

  1. # 输出日记,生产环境会输出到指定目录
  2. logger.info("[Hello Word] 该 APP 执行参数为: {name}", name=name)
  3. # 返回值,格式为: 状态码,返回内容
  4. return {"status": 0, "result": "Hello," + name}
  1. **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 模块”

  1. # 使用 await
  2. r = await requests.get(url="https://w5.io")
  3. print(r)
  4. logger.info("[Hello Word] 该 APP 执行参数为: {name}", name=name)
  5. return {"status":0,"result":"Hello," + name,"html": '''<span style="color:red">{name}</span>'''.format(name="Hello," + name)}
  1. ## APP 测试
  2. 使用 **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):

  1. # 输出日记,生产环境会输出到指定目录
  2. logger.info("[Hello Word] 该 APP 执行参数为: {name}", name=name)
  3. # 返回值,格式为: 状态码,返回内容
  4. return {"status": 0, "result": "Hello," + name}

if name == ‘main‘:

  1. # 导入异步库
  2. import asyncio
  3. # 测试函数
  4. async def test():
  5. result = await hello_word("W5")
  6. print(result)
  7. # 加入异步队列
  8. async def main(): await asyncio.gather(test())
  9. # 启动执行
  10. asyncio.run(main())

```

执行演示

dev_1