class AsyncTransport(Transport): async_mode = True sync_transport = Transport def __init__(self, *args, **kwargs): super(AsyncTransport, self).__init__(*args, **kwargs) self._worker = None @property def worker(self): if not self._worker or not self._worker.is_alive(): self._worker = AsyncWorker() return self._worker def send_sync(self, data=None): try: self.sync_transport.send(self, data) self.handle_transport_success() except Exception as e: self.handle_transport_fail(exception=e) def send_async(self, data): self.worker.queue(self.send_sync, {"data": data}) def close(self): super(AsyncTransport, self).close() if self._worker: self._worker.main_thread_terminated()
class AsyncHTTPTransportBase(AsyncTransport, HTTPTransportBase): scheme = ["http", "https"] async_mode = True sync_transport = HTTPTransportBase def __init__(self, parsed_url, **kwargs): super(AsyncHTTPTransportBase, self).__init__(parsed_url, **kwargs) if self._url.startswith("async+"): self._url = self._url[6:] self._worker = None @property def worker(self): if not self._worker or not self._worker.is_alive(): self._worker = AsyncWorker() return self._worker def send_sync(self, data=None, headers=None, success_callback=None, fail_callback=None): try: url = self.sync_transport.send(self, data, headers) if callable(success_callback): success_callback(url=url) except Exception as e: if callable(fail_callback): fail_callback(exception=e) def send_async(self, data, headers, success_callback=None, fail_callback=None): kwargs = { "data": data, "headers": headers, "success_callback": success_callback, "fail_callback": fail_callback, } self.worker.queue(self.send_sync, kwargs) def close(self): if self._worker: self._worker.main_thread_terminated()
class AsyncHTTPTransport(AsyncTransport, HTTPTransport): scheme = ['http', 'https'] async_mode = True def __init__(self, parsed_url): super(AsyncHTTPTransport, self).__init__(parsed_url) if self._url.startswith('async+'): self._url = self._url[6:] self._worker = None @property def worker(self): if not self._worker or not self._worker.is_alive(): self._worker = AsyncWorker() return self._worker def send_sync(self, data=None, headers=None, success_callback=None, fail_callback=None): try: url = HTTPTransport.send(self, data, headers) if callable(success_callback): success_callback(url=url) except Exception as e: if callable(fail_callback): fail_callback(exception=e) def send_async(self, data, headers, success_callback=None, fail_callback=None): kwargs = { 'data': data, 'headers': headers, 'success_callback': success_callback, 'fail_callback': fail_callback, } self.worker.queue(self.send_sync, kwargs) def close(self): if self._worker: self._worker.main_thread_terminated()
def worker(self): if not self._worker or not self._worker.is_alive(): self._worker = AsyncWorker() return self._worker