Beispiel #1
0
    async def test_aiodump(self):
        async with filesystem.aioopen(test_file, "w") as fp:
            await aiojson.aiodump(test_data_obj, fp)

        with open(test_file) as fp:
            data = fp.read()
            self.assertEqual(test_data_raw, data)
Beispiel #2
0
 async def _unsafe_write(self) -> None:
     async with filesystem.aioopen(self._file_name, "w") as fp:
         start = time.monotonic()
         await aiojson.aiodump(self._cache, fp)
         end = time.monotonic()
         self.log.info(
             f"Serialized to {self._file_name} in {(end - start) * 1_000_000:.2f}µs"
         )
Beispiel #3
0
    async def test_aioload(self):
        with open(test_file, "w") as fp:
            fp.write(test_data_raw)

        async with filesystem.aioopen(test_file) as fp:
            obj = await aiojson.aioload(fp)

        self.assertEqual(test_data_obj, obj)
Beispiel #4
0
    async def read_data_from_disk(self) -> None:
        """Reads the most recent data stored on disk, replacing anything currently in memory."""
        if not os.path.exists(self._file_name):
            await self._unsafe_write()

        async with self._lock, filesystem.aioopen(self._file_name) as fp:
            start = time.monotonic()
            obj = await aiojson.aioload(fp)
            end = time.monotonic()

            if not isinstance(obj, dict):
                raise TypeError(f"Expected dict in {self._file_name}, "
                                f"received {type(obj).__name__}")

            self.log.info(
                f"Serialized from {self._file_name} in {(end - start) * 1_000_000:.2f}µs"
            )

            self._cache = obj