@@ -104,16 +104,18 @@ def _request_wrapper(
104
104
if token :
105
105
payload ["token" ] = token
106
106
107
- # Set lock
108
- request_lock .acquire ()
109
-
110
107
try :
108
+ # Set lock
109
+ request_lock .acquire ()
111
110
112
111
# Wait if needed
113
112
with cooldown_timer .get_lock ():
114
113
cooldown_timer_ = cooldown_timer .value
115
- while time .time () - cooldown_timer_ < REQUEST_COOLDOWN :
116
- time .sleep (0.1 )
114
+ if time .time () - cooldown_timer_ < REQUEST_COOLDOWN :
115
+ logging .info ("Waiting for cooldown before sending request" )
116
+ while time .time () - cooldown_timer_ < REQUEST_COOLDOWN :
117
+ time .sleep (0.1 )
118
+ logging .info ("Cooldown passed" )
117
119
118
120
with cooldown_timer .get_lock ():
119
121
cooldown_timer .value = time .time ()
@@ -372,7 +374,6 @@ def _lmao_stop_stream_loop() -> None:
372
374
logging .warning (f"Exit from { name } loop requested" )
373
375
break
374
376
375
- release_lock = False
376
377
request_response = None
377
378
378
379
try :
@@ -438,79 +439,83 @@ def _lmao_stop_stream_loop() -> None:
438
439
users_handler_ .set_key (request_response .user_id , "suggestions" , [])
439
440
440
441
# Ask and read stream
441
- release_lock = True
442
- for line in _request_wrapper (
443
- api_url ,
444
- "ask" ,
445
- {name_lmao : module_request },
446
- cooldown_timer ,
447
- request_lock ,
448
- proxy ,
449
- token ,
450
- stream = True ,
451
- ).iter_lines ():
452
- if not line :
453
- continue
454
- try :
455
- response = json .loads (line .decode ("utf-8" ))
456
- except Exception as e :
457
- logging .warning (f"Unable to parse response line as JSON: { e } " )
458
- continue
459
-
460
- finished = response .get ("finished" )
461
- conversation_id = response .get ("conversation_id" )
462
- request_response .response_text = response .get ("response" )
463
-
464
- images = response .get ("images" )
465
- if images is not None :
466
- request_response .response_images = images [:]
467
-
468
- # Format and add attributions
469
- attributions = response .get ("attributions" )
470
- if attributions is not None and len (attributions ) != 0 :
471
- response_link_format = messages_ .get_message (
472
- "response_link_format" , user_id = request_response .user_id
473
- )
474
- request_response .response_text += "\n "
475
- for i , attribution in enumerate (attributions ):
476
- request_response .response_text += response_link_format .format (
477
- source_name = str (i + 1 ), link = attribution .get ("url" , "" )
442
+ try :
443
+ for line in _request_wrapper (
444
+ api_url ,
445
+ "ask" ,
446
+ {name_lmao : module_request },
447
+ cooldown_timer ,
448
+ request_lock ,
449
+ proxy ,
450
+ token ,
451
+ stream = True ,
452
+ ).iter_lines ():
453
+ if not line :
454
+ continue
455
+ try :
456
+ response = json .loads (line .decode ("utf-8" ))
457
+ except Exception as e :
458
+ logging .warning (f"Unable to parse response line as JSON: { e } " )
459
+ continue
460
+
461
+ finished = response .get ("finished" )
462
+ conversation_id = response .get ("conversation_id" )
463
+ request_response .response_text = response .get ("response" )
464
+
465
+ images = response .get ("images" )
466
+ if images is not None :
467
+ request_response .response_images = images [:]
468
+
469
+ # Format and add attributions
470
+ attributions = response .get ("attributions" )
471
+ if attributions is not None and len (attributions ) != 0 :
472
+ response_link_format = messages_ .get_message (
473
+ "response_link_format" , user_id = request_response .user_id
478
474
)
475
+ request_response .response_text += "\n "
476
+ for i , attribution in enumerate (attributions ):
477
+ request_response .response_text += response_link_format .format (
478
+ source_name = str (i + 1 ), link = attribution .get ("url" , "" )
479
+ )
479
480
480
- # Suggestions must be stored as tuples with unique ID for reply-markup
481
- if finished :
482
- suggestions = response .get ("suggestions" )
483
- if suggestions is not None :
484
- request_response .response_suggestions = []
485
- for suggestion in suggestions :
486
- if not suggestion or len (suggestion ) < 1 :
487
- continue
488
- id_ = "" .join (
489
- random .choices (
490
- string .ascii_uppercase + string .ascii_lowercase + string .digits , k = 8
481
+ # Suggestions must be stored as tuples with unique ID for reply-markup
482
+ if finished :
483
+ suggestions = response .get ("suggestions" )
484
+ if suggestions is not None :
485
+ request_response .response_suggestions = []
486
+ for suggestion in suggestions :
487
+ if not suggestion or len (suggestion ) < 1 :
488
+ continue
489
+ id_ = "" .join (
490
+ random .choices (
491
+ string .ascii_uppercase + string .ascii_lowercase + string .digits , k = 8
492
+ )
491
493
)
494
+ request_response .response_suggestions .append ((id_ , suggestion ))
495
+ users_handler_ .set_key (
496
+ request_response .user_id ,
497
+ "suggestions" ,
498
+ request_response .response_suggestions ,
492
499
)
493
- request_response .response_suggestions .append ((id_ , suggestion ))
494
- users_handler_ .set_key (
495
- request_response .user_id ,
496
- "suggestions" ,
497
- request_response .response_suggestions ,
498
- )
499
500
500
- # Check if exit was requested
501
- with lmao_process_running .get_lock ():
502
- lmao_process_running_value = lmao_process_running .value
503
- if not lmao_process_running_value :
504
- finished = True
501
+ # Check if exit was requested
502
+ with lmao_process_running .get_lock ():
503
+ lmao_process_running_value = lmao_process_running .value
504
+ if not lmao_process_running_value :
505
+ finished = True
506
+
507
+ # Send response to the user
508
+ async_helper (
509
+ send_message_async (config .get ("telegram" ), messages_ , request_response , end = finished )
510
+ )
505
511
506
- # Send response to the user
507
- async_helper (
508
- send_message_async (config .get ("telegram" ), messages_ , request_response , end = finished )
509
- )
512
+ # Exit from stream reader
513
+ if not lmao_process_running_value :
514
+ break
510
515
511
- # Exit from stream reader
512
- if not lmao_process_running_value :
513
- break
516
+ # Release lock here, because _request_wrapper will not do it for stream response
517
+ finally :
518
+ request_lock . release ()
514
519
515
520
# Save conversation ID
516
521
logging .info (f"Saving user { request_response .user_id } conversation ID as: name_{ conversation_id } " )
@@ -577,11 +582,8 @@ def _lmao_stop_stream_loop() -> None:
577
582
logging .error (f"{ name } error" , exc_info = e )
578
583
lmao_exceptions_queue .put (e )
579
584
580
- # Release lock if needed and return the container
585
+ # Return the container
581
586
finally :
582
- if release_lock :
583
- request_lock .release ()
584
- release_lock = False
585
587
if request_response :
586
588
lmao_response_queue .put (request_response )
587
589
@@ -609,7 +611,7 @@ def _lmao_stop_stream_loop() -> None:
609
611
logging .info (f"Trying to close { name } " )
610
612
try :
611
613
# Request module close
612
- _request_wrapper (api_url , "delete " , {"module" : name_lmao }, cooldown_timer , request_lock , proxy , token )
614
+ _request_wrapper (api_url , "close " , {"module" : name_lmao }, cooldown_timer , request_lock , proxy , token )
613
615
614
616
# Wait
615
617
logging .info (f"Waiting for { name_lmao } module to close" )
0 commit comments