Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
thread.py54 linesDownload Raw Back to threaded
1"""Module containing the SessionThread class."""2import threading3import uuid4 5import requests.exceptions as exc6 7from .._compat import queue8 9 10class SessionThread(object):11    def __init__(self, initialized_session, job_queue, response_queue,12                 exception_queue):13        self._session = initialized_session14        self._jobs = job_queue15        self._create_worker()16        self._responses = response_queue17        self._exceptions = exception_queue18 19    def _create_worker(self):20        self._worker = threading.Thread(21            target=self._make_request,22            name=uuid.uuid4(),23        )24        self._worker.daemon = True25        self._worker._state = 026        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                break44 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()54 
codekingpro/portable-devtools · Team Ai