@@ -72,8 +72,8 @@ async def coro(name, timeout):
72
72
s = await fastapi_plugins .scheduler_plugin ()
73
73
# import random
74
74
for i in range (10 ):
75
- await s .spawn (coro (str (i ), i / 10 ))
76
- # await s.spawn(coro(str(i), i/10 + random.choice([0.1, 0.2, 0.3, 0.4, 0.5]))) # nosec B311
75
+ await s .spawn (coro (str (i ), i / 10 ))
76
+ # await s.spawn(coro(str(i), i/10 + random.choice([0.1, 0.2, 0.3, 0.4, 0.5]))) # nosec B311 # noqa
77
77
# print('----------')
78
78
print ('- sleep' , 5 )
79
79
await asyncio .sleep (5.0 )
@@ -148,18 +148,18 @@ async def coro(con, name, timeout):
148
148
num_sleep = 0.25
149
149
150
150
print ('- play' )
151
- l = await fastapi_plugins .log_plugin ()
151
+ logger = await fastapi_plugins .log_plugin ()
152
152
c = await fastapi_plugins .redis_plugin ()
153
153
s = await fastapi_plugins .scheduler_plugin ()
154
154
for i in range (num_jobs ):
155
- await s .spawn (coro (c , str (i ), i / 10 ))
156
- l .info ('- sleep %s' % num_sleep )
155
+ await s .spawn (coro (c , str (i ), i / 10 ))
156
+ logger .info ('- sleep %s' % num_sleep )
157
157
# print('- sleep', num_sleep)
158
158
await asyncio .sleep (num_sleep )
159
- l .info ('- check' )
159
+ logger .info ('- check' )
160
160
# print('- check')
161
161
for i in range (num_jobs ):
162
- l .info ('%s == %s' % (i , await c .get (str (i ))))
162
+ logger .info ('%s == %s' % (i , await c .get (str (i ))))
163
163
# print(i, '==', await c.get(str(i)))
164
164
finally :
165
165
print ('- terminate' )
@@ -186,9 +186,9 @@ class CustomLoggingSettings(fastapi_plugins.LoggingSettings):
186
186
187
187
class CustomLoggingPlugin (fastapi_plugins .LoggingPlugin ):
188
188
def _create_logger (
189
- self ,
190
- name :str ,
191
- config :pydantic_settings .BaseSettings = None
189
+ self ,
190
+ name : str ,
191
+ config : pydantic_settings .BaseSettings = None
192
192
) -> logging .Logger :
193
193
import sys
194
194
handler = logging .StreamHandler (stream = sys .stderr )
@@ -227,18 +227,18 @@ class AppSettings(
227
227
num_sleep = 0.25
228
228
229
229
print ('- play' )
230
- l = await mylog_plugin ()
230
+ logger = await mylog_plugin ()
231
231
c = await fastapi_plugins .redis_plugin ()
232
232
s = await fastapi_plugins .scheduler_plugin ()
233
233
for i in range (num_jobs ):
234
- await s .spawn (coro (c , str (i ), i / 10 ))
235
- l .info ('- sleep %s' % num_sleep )
234
+ await s .spawn (coro (c , str (i ), i / 10 ))
235
+ logger .info ('- sleep %s' % num_sleep )
236
236
# print('- sleep', num_sleep)
237
237
await asyncio .sleep (num_sleep )
238
- l .info ('- check' )
238
+ logger .info ('- check' )
239
239
# print('- check')
240
240
for i in range (num_jobs ):
241
- l .info ('%s == %s' % (i , await c .get (str (i ))))
241
+ logger .info ('%s == %s' % (i , await c .get (str (i ))))
242
242
# print(i, '==', await c.get(str(i)))
243
243
finally :
244
244
print ('- terminate' )
@@ -248,6 +248,104 @@ class AppSettings(
248
248
print ('---demo done' )
249
249
250
250
251
+ async def test_demo_orjson_log ():
252
+ async def coro (con , name , timeout ):
253
+ try :
254
+ await con .set (name , '...' )
255
+ print ('> sleep' , name , timeout )
256
+ await asyncio .sleep (timeout )
257
+ await con .set (name , 'done' )
258
+ print ('---> sleep done' , name , timeout )
259
+ except asyncio .CancelledError as e :
260
+ print ('coro cancelled' , name )
261
+ raise e
262
+
263
+ print ('--- do demo' )
264
+ app = fastapi_plugins .register_middleware (fastapi .FastAPI ())
265
+ config = AppSettings (logging_style = fastapi_plugins .LoggingStyle .logorjson )
266
+
267
+ await fastapi_plugins .log_plugin .init_app (app , config , name = __name__ )
268
+ await fastapi_plugins .log_plugin .init ()
269
+ await fastapi_plugins .redis_plugin .init_app (app = app , config = config )
270
+ await fastapi_plugins .redis_plugin .init ()
271
+ await fastapi_plugins .scheduler_plugin .init_app (app = app , config = config )
272
+ await fastapi_plugins .scheduler_plugin .init ()
273
+
274
+ try :
275
+ num_jobs = 10
276
+ num_sleep = 0.25
277
+
278
+ print ('- play' )
279
+ logger = await fastapi_plugins .log_plugin ()
280
+ c = await fastapi_plugins .redis_plugin ()
281
+ s = await fastapi_plugins .scheduler_plugin ()
282
+ for i in range (num_jobs ):
283
+ await s .spawn (coro (c , str (i ), i / 10 ))
284
+ logger .info ('- sleep %s' % num_sleep , extra = dict (bla = 'bla' ))
285
+ # print('- sleep', num_sleep)
286
+ await asyncio .sleep (num_sleep )
287
+ logger .info ('- check' )
288
+ # print('- check')
289
+ for i in range (num_jobs ):
290
+ logger .info ('%s == %s' % (i , await c .get (str (i ))))
291
+ # print(i, '==', await c.get(str(i)))
292
+ finally :
293
+ print ('- terminate' )
294
+ await fastapi_plugins .scheduler_plugin .terminate ()
295
+ await fastapi_plugins .redis_plugin .terminate ()
296
+ await fastapi_plugins .log_plugin .terminate ()
297
+ print ('---demo done' )
298
+
299
+
300
+ async def test_demo_json_log ():
301
+ async def coro (con , name , timeout ):
302
+ try :
303
+ await con .set (name , '...' )
304
+ print ('> sleep' , name , timeout )
305
+ await asyncio .sleep (timeout )
306
+ await con .set (name , 'done' )
307
+ print ('---> sleep done' , name , timeout )
308
+ except asyncio .CancelledError as e :
309
+ print ('coro cancelled' , name )
310
+ raise e
311
+
312
+ print ('--- do demo' )
313
+ app = fastapi_plugins .register_middleware (fastapi .FastAPI ())
314
+ config = AppSettings (logging_style = fastapi_plugins .LoggingStyle .logjson )
315
+
316
+ await fastapi_plugins .log_plugin .init_app (app , config , name = __name__ )
317
+ await fastapi_plugins .log_plugin .init ()
318
+ await fastapi_plugins .redis_plugin .init_app (app = app , config = config )
319
+ await fastapi_plugins .redis_plugin .init ()
320
+ await fastapi_plugins .scheduler_plugin .init_app (app = app , config = config )
321
+ await fastapi_plugins .scheduler_plugin .init ()
322
+
323
+ try :
324
+ num_jobs = 10
325
+ num_sleep = 0.25
326
+
327
+ print ('- play' )
328
+ logger = await fastapi_plugins .log_plugin ()
329
+ c = await fastapi_plugins .redis_plugin ()
330
+ s = await fastapi_plugins .scheduler_plugin ()
331
+ for i in range (num_jobs ):
332
+ await s .spawn (coro (c , str (i ), i / 10 ))
333
+ logger .info ('- sleep %s' % num_sleep , extra = dict (bla = 'bla' ))
334
+ # print('- sleep', num_sleep)
335
+ await asyncio .sleep (num_sleep )
336
+ logger .info ('- check' )
337
+ # print('- check')
338
+ for i in range (num_jobs ):
339
+ logger .info ('%s == %s' % (i , await c .get (str (i ))))
340
+ # print(i, '==', await c.get(str(i)))
341
+ finally :
342
+ print ('- terminate' )
343
+ await fastapi_plugins .scheduler_plugin .terminate ()
344
+ await fastapi_plugins .redis_plugin .terminate ()
345
+ await fastapi_plugins .log_plugin .terminate ()
346
+ print ('---demo done' )
347
+
348
+
251
349
async def test_memcached ():
252
350
print ('---test memcached' )
253
351
from fastapi_plugins .memcached import MemcachedSettings
@@ -261,7 +359,7 @@ class MoreSettings(AppSettings, MemcachedSettings):
261
359
config = MoreSettings ()
262
360
await memcached_plugin .init_app (app = app , config = config )
263
361
await memcached_plugin .init ()
264
-
362
+
265
363
c = await memcached_plugin ()
266
364
print (await c .get (b'x' ))
267
365
print (await c .set (b'x' , str (time .time ()).encode ()))
@@ -270,6 +368,51 @@ class MoreSettings(AppSettings, MemcachedSettings):
270
368
print ('---test memcached done' )
271
369
272
370
371
+ async def test_demo_buffered_log ():
372
+ async def coro (con , name , timeout ):
373
+ try :
374
+ await con .set (name , '...' )
375
+ print ('> sleep' , name , timeout )
376
+ await asyncio .sleep (timeout )
377
+ await con .set (name , 'done' )
378
+ print ('---> sleep done' , name , timeout )
379
+ except asyncio .CancelledError as e :
380
+ print ('coro cancelled' , name )
381
+ raise e
382
+
383
+ print ('--- do demo' )
384
+ app = fastapi_plugins .register_middleware (fastapi .FastAPI ())
385
+ config = AppSettings (
386
+ logging_style = fastapi_plugins .LoggingStyle .logjson ,
387
+ logging_memory_capacity = 25
388
+ )
389
+
390
+ await fastapi_plugins .log_plugin .init_app (app , config , name = __name__ )
391
+ await fastapi_plugins .log_plugin .init ()
392
+ await fastapi_plugins .redis_plugin .init_app (app = app , config = config )
393
+ await fastapi_plugins .redis_plugin .init ()
394
+ await fastapi_plugins .scheduler_plugin .init_app (app = app , config = config )
395
+ await fastapi_plugins .scheduler_plugin .init ()
396
+
397
+ try :
398
+ num_jobs = 10
399
+ num_tasks = 30
400
+
401
+ logger = await fastapi_plugins .log_plugin ()
402
+ for c_job in range (num_jobs ):
403
+ print (f'----- JOB { c_job } ' )
404
+ for c_task in range (num_tasks ):
405
+ logger .info (f'job={ c_job } task={ c_task } hello world' )
406
+ await asyncio .sleep (0.1 )
407
+ await asyncio .sleep (0.5 )
408
+ finally :
409
+ print ('- terminate' )
410
+ await fastapi_plugins .scheduler_plugin .terminate ()
411
+ await fastapi_plugins .redis_plugin .terminate ()
412
+ await fastapi_plugins .log_plugin .terminate ()
413
+ print ('---demo done' )
414
+
415
+
273
416
# =============================================================================
274
417
# ---
275
418
# =============================================================================
@@ -301,21 +444,49 @@ def main_demo():
301
444
loop = asyncio .get_event_loop ()
302
445
loop .run_until_complete (test_demo ())
303
446
447
+
304
448
def main_demo_custom_log ():
305
449
print (os .linesep * 3 )
306
450
print ('=' * 50 )
451
+ print ('= DEMO CUSTOM LOG' )
307
452
loop = asyncio .get_event_loop ()
308
453
loop .run_until_complete (test_demo_custom_log ())
309
454
310
455
456
+ def main_demo_json_log ():
457
+ print (os .linesep * 3 )
458
+ print ('=' * 50 )
459
+ print ('= DEMO JSON LOG' )
460
+ loop = asyncio .get_event_loop ()
461
+ loop .run_until_complete (test_demo_json_log ())
462
+
463
+
464
+ def main_demo_orjson_log ():
465
+ print (os .linesep * 3 )
466
+ print ('=' * 50 )
467
+ print ('= DEMO ORJSON LOG' )
468
+ loop = asyncio .get_event_loop ()
469
+ loop .run_until_complete (test_demo_orjson_log ())
470
+
471
+
472
+ def main_demo_buffered_log ():
473
+ print (os .linesep * 3 )
474
+ print ('=' * 50 )
475
+ print ('= DEMO BUFFERED LOG' )
476
+ loop = asyncio .get_event_loop ()
477
+ loop .run_until_complete (test_demo_buffered_log ())
478
+
479
+
311
480
if __name__ == '__main__' :
312
481
main_redis ()
313
482
main_scheduler ()
314
483
main_demo ()
315
484
main_demo_custom_log ()
316
- #
485
+ main_demo_json_log ()
486
+ main_demo_orjson_log ()
487
+ main_demo_buffered_log ()
488
+
317
489
try :
318
490
main_memcached ()
319
491
except Exception as e :
320
492
print (type (e ), e )
321
-
0 commit comments