Procházet zdrojové kódy

[enh] use one single http connection pool : improve response time. close #100

dalf před 10 roky
rodič
revize
d07cfd9089
5 změnil soubory, kde provedl 65 přidání a 5 odebrání
  1. 1
    1
      searx/autocomplete.py
  2. 1
    1
      searx/engines/wikidata.py
  3. 61
    0
      searx/poolrequests.py
  4. 1
    2
      searx/search.py
  5. 1
    1
      searx/webapp.py

+ 1
- 1
searx/autocomplete.py Zobrazit soubor

17
 
17
 
18
 
18
 
19
 from lxml import etree
19
 from lxml import etree
20
-from requests import get
21
 from json import loads
20
 from json import loads
22
 from urllib import urlencode
21
 from urllib import urlencode
23
 from searx.languages import language_codes
22
 from searx.languages import language_codes
24
 from searx.engines import (
23
 from searx.engines import (
25
     categories, engines, engine_shortcuts
24
     categories, engines, engine_shortcuts
26
 )
25
 )
26
+from searx.poolrequests import get
27
 
27
 
28
 
28
 
29
 def searx_bang(full_query):
29
 def searx_bang(full_query):

+ 1
- 1
searx/engines/wikidata.py Zobrazit soubor

1
 import json
1
 import json
2
-from requests import get
3
 from urllib import urlencode
2
 from urllib import urlencode
3
+from searx.poolrequests import get
4
 from searx.utils import format_date_by_locale
4
 from searx.utils import format_date_by_locale
5
 
5
 
6
 result_count = 1
6
 result_count = 1

+ 61
- 0
searx/poolrequests.py Zobrazit soubor

1
+import requests
2
+
3
+
4
+the_http_adapter = requests.adapters.HTTPAdapter(pool_connections=100)
5
+the_https_adapter = requests.adapters.HTTPAdapter(pool_connections=100)
6
+
7
+
8
+class SessionSinglePool(requests.Session):
9
+
10
+    def __init__(self):
11
+        global the_https_adapter, the_http_adapter
12
+        super(SessionSinglePool, self).__init__()
13
+
14
+        # reuse the same adapters
15
+        self.adapters.clear()
16
+        self.mount('https://', the_https_adapter)
17
+        self.mount('http://', the_http_adapter)
18
+
19
+    def close(self):
20
+        """Call super, but clear adapters since there are managed globaly"""
21
+        self.adapters.clear()
22
+        super(SessionSinglePool, self).close()
23
+
24
+
25
+def request(method, url, **kwargs):
26
+    """same as requests/requests/api.py request(...) except it use SessionSinglePool"""
27
+    session = SessionSinglePool()
28
+    response = session.request(method=method, url=url, **kwargs)
29
+    session.close()
30
+    return response
31
+
32
+
33
+def get(url, **kwargs):
34
+    kwargs.setdefault('allow_redirects', True)
35
+    return request('get', url, **kwargs)
36
+
37
+
38
+def options(url, **kwargs):
39
+    kwargs.setdefault('allow_redirects', True)
40
+    return request('options', url, **kwargs)
41
+
42
+
43
+def head(url, **kwargs):
44
+    kwargs.setdefault('allow_redirects', False)
45
+    return request('head', url, **kwargs)
46
+
47
+
48
+def post(url, data=None, json=None, **kwargs):
49
+    return request('post', url, data=data, json=json, **kwargs)
50
+
51
+
52
+def put(url, data=None, **kwargs):
53
+    return request('put', url, data=data, **kwargs)
54
+
55
+
56
+def patch(url, data=None, **kwargs):
57
+    return request('patch', url, data=data, **kwargs)
58
+
59
+
60
+def delete(url, **kwargs):
61
+    return request('delete', url, **kwargs)

+ 1
- 2
searx/search.py Zobrazit soubor

15
 (C) 2013- by Adam Tauber, <asciimoo@gmail.com>
15
 (C) 2013- by Adam Tauber, <asciimoo@gmail.com>
16
 '''
16
 '''
17
 
17
 
18
-import requests as requests_lib
19
 import threading
18
 import threading
20
 import re
19
 import re
20
+import searx.poolrequests as requests_lib
21
 from itertools import izip_longest, chain
21
 from itertools import izip_longest, chain
22
 from operator import itemgetter
22
 from operator import itemgetter
23
 from Queue import Queue
23
 from Queue import Queue
31
 from searx.query import Query
31
 from searx.query import Query
32
 from searx import logger
32
 from searx import logger
33
 
33
 
34
-
35
 logger = logger.getChild('search')
34
 logger = logger.getChild('search')
36
 
35
 
37
 number_of_searches = 0
36
 number_of_searches = 0

+ 1
- 1
searx/webapp.py Zobrazit soubor

27
 import os
27
 import os
28
 
28
 
29
 from datetime import datetime, timedelta
29
 from datetime import datetime, timedelta
30
-from requests import get as http_get
31
 from itertools import chain
30
 from itertools import chain
32
 from urllib import urlencode
31
 from urllib import urlencode
33
 from flask import (
32
 from flask import (
36
 )
35
 )
37
 from flask.ext.babel import Babel, gettext, format_date
36
 from flask.ext.babel import Babel, gettext, format_date
38
 from searx import settings, searx_dir
37
 from searx import settings, searx_dir
38
+from searx.poolrequests import get as http_get
39
 from searx.engines import (
39
 from searx.engines import (
40
     categories, engines, get_engines_stats, engine_shortcuts
40
     categories, engines, get_engines_stats, engine_shortcuts
41
 )
41
 )