-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
75 lines (62 loc) · 1.93 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from flask import Flask, jsonify, request, render_template
import json
import base64
import os
import time
from img_generator import ImgGenerator
import threading
from utils import *
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
# class ImgPool(threading.Thread):
# def __init__(self):
# threading.Thread.__init__(self)
# self.imgs = []
#
# def run(self):
# pass
class ImgPool(threading.Thread):
def __init__(self, size=100) -> None:
threading.Thread.__init__(self)
# super().__init__(self)
self.imgs = []
self.size = size
self.last = ""
self.img_gen = ImgGenerator(get_config()["sdw_url"])
self.sem = threading.Semaphore(size)
def run(self):
while True:
self.sem.acquire()
try:
img, args = self.img_gen.get_random_img()
self.imgs.append((img, args))
except Exception as e:
print(e)
def get_img(self):
if len(self.imgs) == 0:
return self.last
if len(self.imgs) > 0:
ret = self.imgs.pop(0)
self.sem.release()
self.last = ret
return ret
def get_last(self):
return self.last
img_pool = ImgPool(size=300)
img_pool.daemon = True
img_pool.start()
@app.route('/')
def get_picture():
img_stream, args = img_pool.get_img()
# render_template()函数是flask函数,它从模版文件夹templates中呈现给定的模板上下文。
return render_template('index.html',img_stream=img_stream, args=json.dumps(args, indent=4))
folder = "favorite"
@app.route("/like", methods=["GET"])
def like():
last, _ = img_pool.get_last()
filename = f"{time.time_ns()}.png"
open(os.path.join(folder, filename), "wb").write(base64.b64decode(last))
return "ok"
if __name__ == '__main__':
# app.run(host, port, debug, options)
app.run(host="0.0.0.0", port=7861)