app = FastAPI()
lock: Optional[asyncio.Lock] = None
spider = Spider()
@app.get('/xxx')
async def xxx(s: str):
t1 = time()
await lock.acquire()
t2 = time()
r = await spider.check(s)
t3 = time()
lock.release()
t4 = time()
logging.info(f"等待耗时{t2-t1}s,check 耗时{t3-t2}s,归还耗时{t4-t3}s")
return r
async def main():
log_config = generate_logging_dict()
loop = asyncio.get_event_loop()
global lock
lock = asyncio.Lock()
config = Config(app=app, loop=loop, log_config=log_config, workers=1,
host="0.0.0.0", port=8000)
server = Server(config)
tasks = [server.serve()]
await asyncio.gather(*tasks)
await spider.close()
if __name__ == '__main__':
config_root_logger()
asyncio.run(main())
获取 lock 和归还 lock 的耗时居然在 5~8s 吗?这也太奇葩了吧。 选取几次代表性的日志如下:
等待耗时 5.4836273193359375e-06s,check 耗时 1.3153741359710693s,归还耗时 8.58306884765625e-06s
等待耗时 5.9604644775390625e-06s,check 耗时 0.6164107322692871s,归还耗时 1.1205673217773438e-05s
等待耗时 6.67572021484375e-06s,check 耗时 0.5971970558166504s,归还耗时 8.344650268554688e-06s
1
mckelvin 2020-11-29 16:55:22 +08:00 2
单位你没仔细看哦。1e-06 s = 0.000001 s
|
3
laike9m 2020-11-29 17:13:30 +08:00 via Android
🤣
|
4
xiaolinjia 2020-11-29 17:36:25 +08:00
你把大伙都整笑了。
|
5
chaogg OP @xiaolinjia 我自己也笑了
|
6
CallMeReznov 2020-11-29 19:40:31 +08:00
|
7
abersheeran 2020-11-30 09:10:17 +08:00
噗,你要是用了 time_ns(),应该就能感觉出来不太对了。
|