質問編集履歴
2
記入漏れの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -272,6 +272,30 @@
|
|
272
272
|
|
273
273
|
```
|
274
274
|
|
275
|
+
```python
|
276
|
+
|
277
|
+
# database/cruds/user.py
|
278
|
+
|
279
|
+
from sqlalchemy.orm import Session
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
from database import models
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
from typing import List
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
def get_users(db: Session, skip: int = 0, limit: int = 100) -> List[models.User]:
|
294
|
+
|
295
|
+
return db.query(models.User).offset(skip).limit(limit).all()
|
296
|
+
|
297
|
+
```
|
298
|
+
|
275
299
|
|
276
300
|
|
277
301
|
■ API実行用
|
1
最小構成にて「試したこと」追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -100,6 +100,212 @@
|
|
100
100
|
|
101
101
|
|
102
102
|
|
103
|
+
下記のように構成を小さくして再度試してみました。
|
104
|
+
|
105
|
+
結果的にはやはりプロセスが残っており、上限?の70プロセスで毎回エラーとなります。
|
106
|
+
|
107
|
+
ただし、DBアクセスを行わない'/'へのアクセスではプロセスの残りはなかったので、
|
108
|
+
|
109
|
+
DB周りが怪しいということが分かりました。closeはちゃんとしているんですが。。。
|
110
|
+
|
111
|
+
■ FastAPI側
|
112
|
+
|
113
|
+
```python
|
114
|
+
|
115
|
+
# main.py
|
116
|
+
|
117
|
+
from fastapi import Depends, FastAPI, Request, Response
|
118
|
+
|
119
|
+
from fastapi.middleware.cors import CORSMiddleware
|
120
|
+
|
121
|
+
from sqlalchemy.orm import Session
|
122
|
+
|
123
|
+
from typing import List
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
from database import models, schemas
|
128
|
+
|
129
|
+
from database.database import SessionLocal, engine
|
130
|
+
|
131
|
+
from database.cruds import user as crud_user
|
132
|
+
|
133
|
+
from dependencies import get_db
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
models.Base.metadata.create_all(bind=engine)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
app = FastAPI()
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
app.add_middleware(
|
146
|
+
|
147
|
+
CORSMiddleware,
|
148
|
+
|
149
|
+
allow_origins=["*"],
|
150
|
+
|
151
|
+
allow_credentials=True,
|
152
|
+
|
153
|
+
allow_methods=["*"],
|
154
|
+
|
155
|
+
allow_headers=["*"],
|
156
|
+
|
157
|
+
)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
@app.middleware('http')
|
164
|
+
|
165
|
+
async def db_session_middleware(request: Request, call_next):
|
166
|
+
|
167
|
+
response = Response('Internal server error', status_code=500)
|
168
|
+
|
169
|
+
try:
|
170
|
+
|
171
|
+
request.state.db = SessionLocal()
|
172
|
+
|
173
|
+
response = await call_next(request)
|
174
|
+
|
175
|
+
finally:
|
176
|
+
|
177
|
+
request.state.db.close()
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
return response
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
@app.get("/")
|
188
|
+
|
189
|
+
async def root():
|
190
|
+
|
191
|
+
return {"message": "Hello World!"}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
@app.get("/users", response_model=List[schemas.User])
|
198
|
+
|
199
|
+
async def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
200
|
+
|
201
|
+
users = crud_user.get_users(db, skip=skip, limit=limit)
|
202
|
+
|
203
|
+
return users
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
```
|
208
|
+
|
209
|
+
```python
|
210
|
+
|
211
|
+
# dependencies.py
|
212
|
+
|
213
|
+
from fastapi import Request
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
def get_db(request: Request):
|
220
|
+
|
221
|
+
return request.state.db
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
```
|
226
|
+
|
227
|
+
```python
|
228
|
+
|
229
|
+
# database/database.py
|
230
|
+
|
231
|
+
from sqlalchemy import create_engine
|
232
|
+
|
233
|
+
from sqlalchemy.ext.declarative import declarative_base
|
234
|
+
|
235
|
+
from sqlalchemy.orm import sessionmaker
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
import json
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
with open('config.json', mode='r') as f:
|
244
|
+
|
245
|
+
config = json.load(f)
|
246
|
+
|
247
|
+
database_info = config['database']
|
248
|
+
|
249
|
+
DATABASE_URL = "mysql+pymysql://%s:%s@%s/%s?charset=utf8mb4" % (
|
250
|
+
|
251
|
+
database_info['user'],
|
252
|
+
|
253
|
+
database_info['password'],
|
254
|
+
|
255
|
+
database_info['host'],
|
256
|
+
|
257
|
+
database_info['db'],
|
258
|
+
|
259
|
+
)
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
engine = create_engine(DATABASE_URL, echo=False, pool_recycle=60)
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
Base = declarative_base()
|
272
|
+
|
273
|
+
```
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
■ API実行用
|
278
|
+
|
279
|
+
テスト用に作ったもので実際はReactからaxiosで実行しています。
|
280
|
+
|
281
|
+
```python
|
282
|
+
|
283
|
+
import subprocess
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
def main():
|
290
|
+
|
291
|
+
for i in range(0, 100):
|
292
|
+
|
293
|
+
print(f'{i+1}回目')
|
294
|
+
|
295
|
+
subprocess.call('curl http://localhost:8001/users', shell=True)
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
if __name__ == '__main__':
|
302
|
+
|
303
|
+
main()
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
```
|
308
|
+
|
103
309
|
### 補足情報(FW/ツールのバージョンなど)
|
104
310
|
|
105
311
|
|