def _reset_output(self): previous_output_file = self.output_file output_path = os.path.join(self.path, datetime.utcnow().isoformat()) # using synchronous open to avoid race condition self.output_file = wrap(open(output_path, mode='bw'), loop=self.loop) if previous_output_file: self.loop.create_task(self._close_output(previous_output_file))
def _open(file, *args, mode="r", loop=None, executor=None, **kwargs): """Wrapping wrapped types that are instantiated in a concurrent generator makes my head hurt; punch the duck!""" if loop is None: loop = asyncio.get_event_loop() partial_function = partial(sync_open, file, *args, mode=mode, **kwargs) file_descriptor = yield from loop.run_in_executor(executor, partial_function) obj = wrap(file_descriptor, loop=loop, executor=executor) # DUCK PUNCH: close() obj._original_close = getattr(obj, "close", None) obj.close = MethodType(duck_punch_close, obj) global _OBJECTS # pylint: disable=global-statement _OBJECTS.append(obj) return obj
def _open(file, *args, mode="r", loop=None, executor=None, **kwargs): """Wrapping wrapped types that are instantiated in a concurrent generator makes my head hurt; punch the duck!""" if loop is None: loop = asyncio.get_event_loop() partial_function = partial(sync_open, file, *args, mode=mode, **kwargs) file_descriptior = yield from loop.run_in_executor(executor, partial_function) baseio = wrap(file_descriptior, loop=loop, executor=executor) # DUCK PUNCH: name baseio.__setattr__("name", str(file)) # DUCK PUNCH: __del__() baseio.__del__ = MethodType(duck_punch___del__, baseio) # DUCK PUNCH: close() baseio._original_close = baseio.close baseio.close = MethodType(duck_punch_close, baseio) return baseio
def test_unsupported_wrap(): """A type error should be raised when wrapping something unsupported.""" with pytest.raises(TypeError): wrap(int)
async def stream_to_queue(file, write_queue): """Function to save streams to queue""" loop = asyncio.get_event_loop() async for message in nbt.wrap(file, loop=loop): # non blocking io read await write_queue.put(message.encode())
async def test_plugin_dispatcher(fs): with open('test', 'w') as f: wrapped = threadpool.wrap(f) assert isinstance(f, fake_filesystem.FakeFileWrapper) assert isinstance(wrapped, base.AsyncBase)