CheeseAPI 0.3.3__tar.gz → 0.3.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/app.py +36 -27
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/handle.py +43 -52
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/signal.py +1 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/PKG-INFO +3 -1
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/README.md +2 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/pyproject.toml +1 -1
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/.gitignore +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/__init__.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/cors.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/exception.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/file.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/module.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/protocol.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/request.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/response.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/route.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/server.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/websocket.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/worker.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/CheeseAPI/workspace.py +0 -0
- {cheeseapi-0.3.3 → cheeseapi-0.3.5}/LICENSE +0 -0
|
@@ -33,37 +33,46 @@ class App:
|
|
|
33
33
|
self.handle: Handle = Handle()
|
|
34
34
|
self.g: Dict[str, Any] = {}
|
|
35
35
|
|
|
36
|
-
def
|
|
36
|
+
def init(self):
|
|
37
37
|
try:
|
|
38
|
-
self.
|
|
39
|
-
manager = multiprocessing.Manager()
|
|
40
|
-
self.managers['lock'] = manager.Lock()
|
|
41
|
-
self.managers['startedWorkerNum'] = manager.Value(int, 0)
|
|
42
|
-
self.managers['firstRequest'] = manager.Value(bool, False)
|
|
43
|
-
|
|
44
|
-
self.handle._server_beforeStartingHandle(self)
|
|
45
|
-
for server_beforeStartingHandle in self.handle.server_beforeStartingHandles:
|
|
46
|
-
server_beforeStartingHandle()
|
|
47
|
-
if signal.receiver('server_beforeStartingHandle'):
|
|
48
|
-
signal.send('server_beforeStartingHandle')
|
|
49
|
-
|
|
50
|
-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
51
|
-
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
52
|
-
sock.bind((self.server.host, self.server.port))
|
|
53
|
-
sock.set_inheritable(True)
|
|
54
|
-
|
|
55
|
-
multiprocessing.allow_connection_pickling()
|
|
56
|
-
for i in range(0, self.server.workers - 1):
|
|
57
|
-
process = multiprocessing.Process(target = run, args = (self, sock), name = f'CheeseAPI_Subprocess<{i}>', daemon = True)
|
|
58
|
-
process.start()
|
|
59
|
-
|
|
60
|
-
run(self, sock, True)
|
|
61
|
-
|
|
62
|
-
while self.managers['startedWorkerNum'].value != 0:
|
|
63
|
-
time.sleep(0.1)
|
|
38
|
+
self.handle._initHandle(self)
|
|
64
39
|
except Exception as e:
|
|
65
40
|
sys.excepthook(Exception, e, sys.exc_info()[2])
|
|
66
41
|
|
|
42
|
+
def run(self, *, managers: Dict[str, multiprocessing.Manager] = {}):
|
|
43
|
+
if 'startTimer' not in app.g:
|
|
44
|
+
logger.error('The app has not yet been initiated!')
|
|
45
|
+
else:
|
|
46
|
+
try:
|
|
47
|
+
self.managers.update(managers)
|
|
48
|
+
manager = multiprocessing.Manager()
|
|
49
|
+
self.managers['lock'] = manager.Lock()
|
|
50
|
+
self.managers['startedWorkerNum'] = manager.Value(int, 0)
|
|
51
|
+
self.managers['firstRequest'] = manager.Value(bool, False)
|
|
52
|
+
|
|
53
|
+
self.handle._server_beforeStartingHandle(self)
|
|
54
|
+
for server_beforeStartingHandle in self.handle.server_beforeStartingHandles:
|
|
55
|
+
server_beforeStartingHandle()
|
|
56
|
+
if signal.receiver('server_beforeStartingHandle'):
|
|
57
|
+
signal.send('server_beforeStartingHandle')
|
|
58
|
+
|
|
59
|
+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
60
|
+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
61
|
+
sock.bind((self.server.host, self.server.port))
|
|
62
|
+
sock.set_inheritable(True)
|
|
63
|
+
|
|
64
|
+
multiprocessing.allow_connection_pickling()
|
|
65
|
+
for i in range(0, self.server.workers - 1):
|
|
66
|
+
process = multiprocessing.Process(target = run, args = (self, sock), name = f'CheeseAPI_Subprocess<{i}>', daemon = True)
|
|
67
|
+
process.start()
|
|
68
|
+
|
|
69
|
+
run(self, sock, True)
|
|
70
|
+
|
|
71
|
+
while self.managers['startedWorkerNum'].value != 0:
|
|
72
|
+
time.sleep(0.1)
|
|
73
|
+
except Exception as e:
|
|
74
|
+
sys.excepthook(Exception, e, sys.exc_info()[2])
|
|
75
|
+
|
|
67
76
|
if signal.receiver('server_beforeStoppingHandle'):
|
|
68
77
|
signal.send('server_beforeStoppingHandle')
|
|
69
78
|
for server_beforeStoppingHandle in self.handle.server_beforeStoppingHandles:
|
|
@@ -2,7 +2,7 @@ import os, time, http, traceback, asyncio
|
|
|
2
2
|
from typing import TYPE_CHECKING, List, Callable, Tuple, Dict, Any
|
|
3
3
|
|
|
4
4
|
import websockets
|
|
5
|
-
from CheeseLog import logger
|
|
5
|
+
from CheeseLog import logger
|
|
6
6
|
from websockets.legacy.server import HTTPResponse
|
|
7
7
|
|
|
8
8
|
from CheeseAPI.response import FileResponse, BaseResponse, Response
|
|
@@ -15,6 +15,7 @@ if TYPE_CHECKING:
|
|
|
15
15
|
|
|
16
16
|
class Handle:
|
|
17
17
|
def __init__(self):
|
|
18
|
+
self.afterInitHandles: List[Callable] = []
|
|
18
19
|
self.server_beforeStartingHandles: List[Callable] = []
|
|
19
20
|
self.worker_beforeStartingHandles: List[Callable] = []
|
|
20
21
|
self.worker_afterStartingHandles: List[Callable] = []
|
|
@@ -68,13 +69,48 @@ class Handle:
|
|
|
68
69
|
def _exitSignalHandle(self, server):
|
|
69
70
|
server.close()
|
|
70
71
|
|
|
72
|
+
def _initHandle(self, app: 'App'):
|
|
73
|
+
app.g['startTimer'] = time.time()
|
|
74
|
+
|
|
75
|
+
moduleNum = len(app.modules)
|
|
76
|
+
if moduleNum:
|
|
77
|
+
for i in range(moduleNum):
|
|
78
|
+
Module(app.modules[i])
|
|
79
|
+
|
|
80
|
+
if app.localModules is True:
|
|
81
|
+
localModule = []
|
|
82
|
+
for foldername in os.listdir(app.workspace.base):
|
|
83
|
+
if foldername[0] == '.' or foldername == '__pycache__':
|
|
84
|
+
continue
|
|
85
|
+
folderPath = os.path.join(app.workspace.base, foldername)
|
|
86
|
+
if os.path.isdir(folderPath):
|
|
87
|
+
if (not app.workspace.static or not os.path.exists(os.path.join(app.workspace.base, app.workspace.static)) or not os.path.samefile(folderPath, os.path.join(app.workspace.base, app.workspace.static))) and (not app.workspace.log or not os.path.exists(os.path.join(app.workspace.base, app.workspace.log)) or not os.path.samefile(folderPath, os.path.join(app.workspace.base, app.workspace.log))) and foldername not in app.exclude_localModules:
|
|
88
|
+
localModule.append(foldername)
|
|
89
|
+
app.localModules = localModule
|
|
90
|
+
else:
|
|
91
|
+
app.preferred_localModules = []
|
|
92
|
+
localModuleNum = len(app.localModules)
|
|
93
|
+
if localModuleNum:
|
|
94
|
+
for module in app.preferred_localModules:
|
|
95
|
+
LocalModule(app.workspace.base, module)
|
|
96
|
+
for module in app.localModules:
|
|
97
|
+
if module not in app.preferred_localModules:
|
|
98
|
+
LocalModule(app.workspace.base, module)
|
|
99
|
+
|
|
100
|
+
if signal.receiver('afterInitHandle'):
|
|
101
|
+
signal.send('afterInitHandle')
|
|
102
|
+
for afterInitHandle in app.handle.afterInitHandles:
|
|
103
|
+
afterInitHandle()
|
|
104
|
+
|
|
105
|
+
def afterInitHandle(self, func: Callable):
|
|
106
|
+
self.afterInitHandles.append(func)
|
|
107
|
+
return func
|
|
108
|
+
|
|
71
109
|
def server_beforeStartingHandle(self, func: Callable):
|
|
72
110
|
self.server_beforeStartingHandles.append(func)
|
|
73
111
|
return func
|
|
74
112
|
|
|
75
113
|
def _server_beforeStartingHandle(self, app: 'App'):
|
|
76
|
-
app.g['startTimer'] = time.time()
|
|
77
|
-
|
|
78
114
|
logger.starting(f'The master process {os.getpid()} started', f'The master process <blue>{os.getpid()}</blue> started')
|
|
79
115
|
|
|
80
116
|
logger.starting(f'''Workspace information:
|
|
@@ -99,55 +135,13 @@ Port: <blue>{app.server.port}</blue>
|
|
|
99
135
|
Workers: <blue>{app.server.workers}</blue>''' + (f'''
|
|
100
136
|
Static: <cyan>{app.server.static}</cyan>''' if app.server.static else ''))
|
|
101
137
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
moduleNum = len(app.modules)
|
|
105
|
-
if moduleNum:
|
|
138
|
+
if app.modules:
|
|
106
139
|
logger.starting(f'''Modules:
|
|
107
|
-
''' + ' | '.join(app.modules)
|
|
108
|
-
''' + ' | '.join(app.modules) + '''
|
|
109
|
-
''')
|
|
110
|
-
timer = time.time()
|
|
111
|
-
for i in range(moduleNum):
|
|
112
|
-
message, styledMessage = progressBar(i * 1.0 / moduleNum)
|
|
113
|
-
logger.loading(message, styledMessage)
|
|
114
|
-
Module(app.modules[i])
|
|
115
|
-
timer = time.time() - timer
|
|
116
|
-
logger.loaded('The modules are loaded, which takes {:.6f} seconds'.format(timer), 'The modules are loaded, which takes <blue>{:.6f}</blue> seconds'.format(timer), refreshed = True)
|
|
140
|
+
''' + ' | '.join(app.modules))
|
|
117
141
|
|
|
118
|
-
if app.localModules
|
|
119
|
-
localModule = []
|
|
120
|
-
for foldername in os.listdir(app.workspace.base):
|
|
121
|
-
if foldername[0] == '.' or foldername == '__pycache__':
|
|
122
|
-
continue
|
|
123
|
-
folderPath = os.path.join(app.workspace.base, foldername)
|
|
124
|
-
if os.path.isdir(folderPath):
|
|
125
|
-
if (not app.workspace.static or not os.path.exists(os.path.join(app.workspace.base, app.workspace.static)) or not os.path.samefile(folderPath, os.path.join(app.workspace.base, app.workspace.static))) and (not app.workspace.log or not os.path.exists(os.path.join(app.workspace.base, app.workspace.log)) or not os.path.samefile(folderPath, os.path.join(app.workspace.base, app.workspace.log))) and foldername not in app.exclude_localModules:
|
|
126
|
-
localModule.append(foldername)
|
|
127
|
-
app.localModules = localModule
|
|
128
|
-
else:
|
|
129
|
-
app.preferred_localModules = []
|
|
130
|
-
localModuleNum = len(app.localModules)
|
|
131
|
-
if localModuleNum:
|
|
142
|
+
if app.localModules:
|
|
132
143
|
logger.starting(f'''Local Modules:
|
|
133
|
-
''' + ' | '.join(app.localModules)
|
|
134
|
-
''' + ' | '.join(app.localModules) + '''
|
|
135
|
-
''')
|
|
136
|
-
timer = time.time()
|
|
137
|
-
count = 0
|
|
138
|
-
for module in app.preferred_localModules:
|
|
139
|
-
message, styledMessage = progressBar(count * 1.0 / localModuleNum)
|
|
140
|
-
logger.loading(message, styledMessage)
|
|
141
|
-
LocalModule(app.workspace.base, module)
|
|
142
|
-
count += 1
|
|
143
|
-
for module in app.localModules:
|
|
144
|
-
if module not in app.preferred_localModules:
|
|
145
|
-
message, styledMessage = progressBar(count * 1.0 / localModuleNum)
|
|
146
|
-
logger.loading(message, styledMessage)
|
|
147
|
-
LocalModule(app.workspace.base, module)
|
|
148
|
-
count += 1
|
|
149
|
-
timer = time.time() - timer
|
|
150
|
-
logger.loaded('The local modules are loaded, which takes {:.6f} seconds'.format(timer), 'The local modules are loaded, which takes <blue>{:.6f}</blue> seconds'.format(timer), refreshed = True)
|
|
144
|
+
''' + ' | '.join(app.localModules))
|
|
151
145
|
|
|
152
146
|
def worker_beforeStartingHandle(self, func: Callable):
|
|
153
147
|
self.worker_beforeStartingHandles.append(func)
|
|
@@ -167,9 +161,6 @@ Static: <cyan>{app.server.static}</cyan>''' if app.server.static else ''))
|
|
|
167
161
|
def _server_afterStartingHandle(self, app: 'App'):
|
|
168
162
|
logger.starting(f'The server started on http://{app.server.host}:{app.server.port}', f'The server started on <cyan><underline>http://{app.server.host}:{app.server.port}</underline></cyan>')
|
|
169
163
|
|
|
170
|
-
timer = time.time() - app.g['startTimer']
|
|
171
|
-
logger.starting('The server startup takes {:.6f} seconds'.format(timer), 'The server startup takes <blue>{:.6f}</blue> seconds'.format(timer))
|
|
172
|
-
|
|
173
164
|
def context_beforeFirstRequestHandle(self, func: Callable):
|
|
174
165
|
self.context_beforeFirstRequestHandles.append(func)
|
|
175
166
|
return func
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: CheeseAPI
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.5
|
|
4
4
|
Summary: 一款web协程框架。
|
|
5
5
|
Project-URL: Source, https://github.com/CheeseUnknown/CheeseAPI
|
|
6
6
|
Author-email: Cheese Unknown <cheese@cheese.ren>
|
|
@@ -52,6 +52,8 @@ from CheeseAPI import app, Response
|
|
|
52
52
|
async def test(**kwargs):
|
|
53
53
|
return Response('您好,这里是CheeseAPI!')
|
|
54
54
|
|
|
55
|
+
app.init() # 初始化app
|
|
56
|
+
|
|
55
57
|
if __name__ == '__main__':
|
|
56
58
|
app.run() # 默认的启动地址:'0.0.0.0',默认的启动端口:5214
|
|
57
59
|
```
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|