[Flask] 01 - Click one button and run one task by Flask

资源收集

  • 入门实操

复现:How to build a web application using Flask and deploy it to the cloud

 

  • 论坛完整案例

基于Python-Flask实现的网站例子

使用Flask写的体育网站 包括(注册 论坛 文章评论 组建战队 后台管理)等功能。

 

  • 教学方案

可以借鉴:greyli/flask-tutorial 

 

  • 适合照片管理:Dahu

Dahu is a picture gallery based on the directory tree of your digital albums. [安装错误]

    • No database needed
    • Automatic generation of thumbnails without editing original files
    • Include a CLI to manage albums and a web frontend
    • Permission management: an album can be public or private or shared by a link

 

  • 照片管理:thumbsup

Ref: https://github.com/thumbsup/thumbsup 

可以借鉴的UI.

 

 

 

 

进入正题


一、与后端数据交互

[UI] 06 - jQuery

RUNOOB.COM jQuery AJAX 方法

 

二、AJAX代码示范

https://github.com/caseydunham/ajax-flask-demo

 

三、综合案例

文章:Developing a Single Page App with Flask and Vue.js

代码:flask-vue-crud

 

 

 

 

任务列表


一、任务

基于上述的"综合案例",任务列表demo如下:

BOOKS = [
    {
        'id': uuid.uuid4().hex,
        'title': 'On the Road',
        'author': 'Jack Kerouac',
        'read': True
    },
    {
        'id': uuid.uuid4().hex,
        'title': 'Harry Potter and the Philosopher\'s Stone',
        'author': 'J. K. Rowling',
        'read': False
    },
    {
        'id': uuid.uuid4().hex,
        'title': 'Green Eggs and Ham',
        'author': 'Dr. Seuss',
        'read': True
    }
]

 

二、Flask 执行任务

Ref: flask+APScheduler 任务调度,计划任务,定时任务

Ref: flask 使用 flask_apscheduler 做定时循环任务

 

三、Huey

Ref: 任务队列调度系统 Huey 简易入门 [去掉redis的密码配置部分,否则遇到了connection问题]

Huey 是一个基于 Python 开发的简易任务队列调度系统。类似于 Celery, 虽然没有 Celery 那么强大,但是胜在简洁。

Python Huey: https://huey.readthedocs.io/en/latest/installation.html

Ref: Huey as crontab alternative in python [可用,已测试]

 

四、更简洁的方案 - rq模块

Ref: Ubuntu 14.04 下安装使用Python rq模块

因为Redis默认使用的端口是6379,该命令可以查看6379端口监听的ip ,可以看到 Redis默认绑定的是127.0.0.1,可以在/etc/redis/redis.conf中看到该设置。

jeff@unsw-ThinkPad-T490:bn_flask$ redis-server
17796:C 18 Aug 11:31:17.846 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17796:C 18 Aug 11:31:17.846 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=17796, just started
17796:C 18 Aug 11:31:17.846 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
17796:M 18 Aug 11:31:17.847 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
17796:M 18 Aug 11:31:17.847 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
17796:M 18 Aug 11:31:17.847 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
17796:M 18 Aug 11:31:17.847 # Creating Server TCP listening socket *:6379: bind: Address already in use
jeff@unsw-ThinkPad-T490:bn_flask$ netstat -an | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:6379          127.0.0.1:59154         ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59164         ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:60850         ESTABLISHED
tcp        0      0 127.0.0.1:59152         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59166         ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59162         ESTABLISHED
tcp        0      0 127.0.0.1:59148         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59158         ESTABLISHED
tcp        0      0 127.0.0.1:59160         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:59154         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59138         ESTABLISHED
tcp        0      0 127.0.0.1:38436         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59148         ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59160         ESTABLISHED
tcp        0      0 127.0.0.1:59168         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:59164         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:59156         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59152         ESTABLISHED
tcp        0      0 127.0.0.1:60848         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59168         ESTABLISHED
tcp        0      0 127.0.0.1:59162         127.0.0.1:6379          ESTABLISHED
tcp        5      0 127.0.0.1:60850         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:59138         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59136         ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59150         ESTABLISHED
tcp        0      0 127.0.0.1:59150         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:38436         ESTABLISHED
tcp        0      0 127.0.0.1:59166         127.0.0.1:6379          ESTABLISHED
tcp        5      0 127.0.0.1:59136         127.0.0.1:6379          ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:60848         ESTABLISHED
tcp        0      0 127.0.0.1:6379          127.0.0.1:59156         ESTABLISHED
tcp        0      0 127.0.0.1:59158         127.0.0.1:6379          ESTABLISHED
tcp6       0      0 ::1:6379                :::*                    LISTEN     
unix  3      [ ]         STREAM     CONNECTED     56379    @/tmp/dbus-7pDPcJFAeC
unix  3      [ ]         STREAM     CONNECTED     26379
jeff@unsw-ThinkPad-T490:bn_flask$ vim /etc/redis/redis.conf

 

  • 可视化

Ref: https://github.com/Parallels/rq-dashboard

$ pip install rq-dashboard
Running the dashboard
Run the dashboard standalone, like this:

$ rq-dashboard
* Running on http://127.0.0.1:9181/
...

 

  • 高级案例

Ref: https://realpython.com/flask-by-example-implementing-a-redis-task-queue/ [其中的代码例子不错]

 

 

  • 快捷案例

Ref: python编程(rq调度系统)

1、安装redis、rq的python库

sudo pip install redis
sudo pip install rq

2、编写work.py

import requests
  
def count_words_at_url(url):
  resp = requests.get(url)
  return len(resp.text.split())

3、编写process.py

from redis import Redis
from rq import Queue
 
q = Queue(connection=Redis())
from work import count_words_at_url result = q.enqueue(count_words_at_url, 'https://www.baidu.com')

4、运行process.py,这个时候只是将工作压进堆栈 

python process.py

5、开启工作进程

rq worker

 

五、视频教程

Python Flask系列(1)——基础 (61课时)

Python Flask系列(2)——进阶 (108课时)

Python Flask系列(3)——实战 (91课时)

 

六、实战教程 

Goto: https://read.helloflask.com/

Grey Li's Watchlist.

 

End.

posted @ 2020-08-13 09:21  郝壹贰叁  阅读(166)  评论(0编辑  收藏  举报