comparison env/lib/python3.7/site-packages/requests_toolbelt/threaded/thread.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 """Module containing the SessionThread class."""
2 import threading
3 import uuid
4
5 import requests.exceptions as exc
6
7 from .._compat import queue
8
9
10 class SessionThread(object):
11 def __init__(self, initialized_session, job_queue, response_queue,
12 exception_queue):
13 self._session = initialized_session
14 self._jobs = job_queue
15 self._create_worker()
16 self._responses = response_queue
17 self._exceptions = exception_queue
18
19 def _create_worker(self):
20 self._worker = threading.Thread(
21 target=self._make_request,
22 name=uuid.uuid4(),
23 )
24 self._worker.daemon = True
25 self._worker._state = 0
26 self._worker.start()
27
28 def _handle_request(self, kwargs):
29 try:
30 response = self._session.request(**kwargs)
31 except exc.RequestException as e:
32 self._exceptions.put((kwargs, e))
33 else:
34 self._responses.put((kwargs, response))
35 finally:
36 self._jobs.task_done()
37
38 def _make_request(self):
39 while True:
40 try:
41 kwargs = self._jobs.get_nowait()
42 except queue.Empty:
43 break
44
45 self._handle_request(kwargs)
46
47 def is_alive(self):
48 """Proxy to the thread's ``is_alive`` method."""
49 return self._worker.is_alive()
50
51 def join(self):
52 """Join this thread to the master thread."""
53 self._worker.join()