File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 1010import settings
1111import plugins
1212from ext import redis_store , cache
13+ from utils import timeout
1314from plugins .utils import convert2str , replaced
1415
1516
@@ -40,6 +41,7 @@ def create_app(config=None):
4041 return app
4142
4243
44+ @timeout (30.0 )
4345def callback (kwargs , app ):
4446 s = convert2str (kwargs ['text' ])
4547 trigger_word = convert2str (kwargs ['trigger_word' ])
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+ from multiprocessing import TimeoutError
4+ from multiprocessing .pool import ThreadPool
5+ from functools import wraps
6+
7+
8+ def timeout (seconds ):
9+ def decorator (fn ):
10+ @wraps (fn )
11+ def wrapper (* args , ** kwargs ):
12+ pool = ThreadPool (processes = 1 )
13+ async_result = pool .apply_async (fn , args = args , kwds = kwargs )
14+ try :
15+ return async_result .get (seconds )
16+ except TimeoutError :
17+ return kwargs .pop ('default' , {'text' : 'timeout' })
18+ return wrapper
19+ return decorator
You can’t perform that action at this time.
0 commit comments