FileNotFoundError Traceback (most recent call last)
Cell In [1], line 24
22 # raises an error, though it works if the number of tasks is small ??
23 tasks = [worker(i) for i in range(100)]
---> 24 foo = await asyncio.gather(*tasks, return_exceptions=False)
Cell In [1], line 14, in worker(i)
12 print(f"Entering {i}")
13 async with httpx_cache.AsyncClient(cache=httpx_cache.FileCache(cache_folder)) as client:
---> 14 result = await client.get("http://www.google.com")
15 print(f"Exiting {i}")
16 return result
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1751, in AsyncClient.get(self, url, params, headers, cookies, auth, follow_redirects, timeout, extensions)
1734 async def get(
1735 self,
1736 url: URLTypes,
(...)
1744 extensions: typing.Optional[dict] = None,
1745 ) -> Response:
1746 """
1747 Send a `GET` request.
1748
1749 **Parameters**: See `httpx.request`.
1750 """
-> 1751 return await self.request(
1752 "GET",
1753 url,
1754 params=params,
1755 headers=headers,
1756 cookies=cookies,
1757 auth=auth,
1758 follow_redirects=follow_redirects,
1759 timeout=timeout,
1760 extensions=extensions,
1761 )
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1527, in AsyncClient.request(self, method, url, content, data, files, json, params, headers, cookies, auth, follow_redirects, timeout, extensions)
1498 """
1499 Build and send a request.
1500
(...)
1512 [0]: /advanced/#merging-of-configuration
1513 """
1514 request = self.build_request(
1515 method=method,
1516 url=url,
(...)
1525 extensions=extensions,
1526 )
-> 1527 return await self.send(request, auth=auth, follow_redirects=follow_redirects)
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1614, in AsyncClient.send(self, request, stream, auth, follow_redirects)
1606 follow_redirects = (
1607 self.follow_redirects
1608 if isinstance(follow_redirects, UseClientDefault)
1609 else follow_redirects
1610 )
1612 auth = self._build_request_auth(request, auth)
-> 1614 response = await self._send_handling_auth(
1615 request,
1616 auth=auth,
1617 follow_redirects=follow_redirects,
1618 history=[],
1619 )
1620 try:
1621 if not stream:
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1642, in AsyncClient._send_handling_auth(self, request, auth, follow_redirects, history)
1639 request = await auth_flow.__anext__()
1641 while True:
-> 1642 response = await self._send_handling_redirects(
1643 request,
1644 follow_redirects=follow_redirects,
1645 history=history,
1646 )
1647 try:
1648 try:
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1679, in AsyncClient._send_handling_redirects(self, request, follow_redirects, history)
1676 for hook in self._event_hooks["request"]:
1677 await hook(request)
-> 1679 response = await self._send_single_request(request)
1680 try:
1681 for hook in self._event_hooks["response"]:
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1716, in AsyncClient._send_single_request(self, request)
1711 raise RuntimeError(
1712 "Attempted to send an sync request with an AsyncClient instance."
1713 )
1715 with request_context(request=request):
-> 1716 response = await transport.handle_async_request(request)
1718 assert isinstance(response.stream, AsyncByteStream)
1719 response.request = request
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx_cache/transport.py:118, in AsyncCacheControlTransport.handle_async_request(self, request)
116 if self.controller.is_request_cacheable(request):
117 logger.debug(f"Checking cache for: {request}")
--> 118 cached_response = await self.cache.aget(request)
119 if cached_response is not None:
120 logger.debug(f"Found cached response for: {request}")
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx_cache/cache/file.py:65, in FileCache.aget(self, request)
63 if await filepath.is_file():
64 async with RWLock().reader:
---> 65 cached = await filepath.read_bytes()
66 return self.serializer.loads(request=request, cached=cached)
67 return None
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/anyio/_core/_fileio.py:505, in Path.read_bytes(self)
504 async def read_bytes(self) -> bytes:
--> 505 return await to_thread.run_sync(self._path.read_bytes)
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/anyio/to_thread.py:31, in run_sync(func, cancellable, limiter, *args)
10 async def run_sync(
11 func: Callable[..., T_Retval],
12 *args: object,
13 cancellable: bool = False,
14 limiter: Optional[CapacityLimiter] = None
15 ) -> T_Retval:
16 """
17 Call the given function with the given arguments in a worker thread.
18
(...)
29
30 """
---> 31 return await get_asynclib().run_sync_in_worker_thread(
32 func, *args, cancellable=cancellable, limiter=limiter
33 )
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/anyio/_backends/_asyncio.py:937, in run_sync_in_worker_thread(func, cancellable, limiter, *args)
935 context.run(sniffio.current_async_library_cvar.set, None)
936 worker.queue.put_nowait((context, func, args, future))
--> 937 return await future
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/anyio/_backends/_asyncio.py:867, in WorkerThread.run(self)
865 exception: Optional[BaseException] = None
866 try:
--> 867 result = context.run(func, *args)
868 except BaseException as exc:
869 exception = exc
File ~/miniconda3/envs/dev/lib/python3.9/pathlib.py:1249, in Path.read_bytes(self)
1245 def read_bytes(self):
1246 """
1247 Open the file in bytes mode, read it, and close the file.
1248 """
-> 1249 with self.open(mode='rb') as f:
1250 return f.read()
File ~/miniconda3/envs/dev/lib/python3.9/pathlib.py:1242, in Path.open(self, mode, buffering, encoding, errors, newline)
1236 def open(self, mode='r', buffering=-1, encoding=None,
1237 errors=None, newline=None):
1238 """
1239 Open the file pointed by this path and return a file object, as
1240 the built-in open() function does.
1241 """
-> 1242 return io.open(self, mode, buffering, encoding, errors, newline,
1243 opener=self._opener)
File ~/miniconda3/envs/dev/lib/python3.9/pathlib.py:1110, in Path._opener(self, name, flags, mode)
1108 def _opener(self, name, flags, mode=0o666):
1109 # A stub for the opener argument to built-in open()
-> 1110 return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'httpx-cache-tests/9ebb612c68d6ddc348478732369ddd52e58ab325704a3beda73e90b8'
On subsequent runs in the same jupyter notebook session, the same code raises some msgpack error:
---------------------------------------------------------------------------
ExtraData Traceback (most recent call last)
Cell In [3], line 23
19 foo = await asyncio.gather(*tasks, return_exceptions=False)
22 tasks = [worker(i) for i in range(100)]
---> 23 foo = await asyncio.gather(*tasks, return_exceptions=False)
Cell In [3], line 14, in worker(i)
12 print(f"Entering {i}")
13 async with httpx_cache.AsyncClient(cache=httpx_cache.FileCache(cache_folder)) as client:
---> 14 result = await client.get("http://www.google.com")
15 print(f"Exiting {i}")
16 return result
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1751, in AsyncClient.get(self, url, params, headers, cookies, auth, follow_redirects, timeout, extensions)
1734 async def get(
1735 self,
1736 url: URLTypes,
(...)
1744 extensions: typing.Optional[dict] = None,
1745 ) -> Response:
1746 """
1747 Send a `GET` request.
1748
1749 **Parameters**: See `httpx.request`.
1750 """
-> 1751 return await self.request(
1752 "GET",
1753 url,
1754 params=params,
1755 headers=headers,
1756 cookies=cookies,
1757 auth=auth,
1758 follow_redirects=follow_redirects,
1759 timeout=timeout,
1760 extensions=extensions,
1761 )
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1527, in AsyncClient.request(self, method, url, content, data, files, json, params, headers, cookies, auth, follow_redirects, timeout, extensions)
1498 """
1499 Build and send a request.
1500
(...)
1512 [0]: /advanced/#merging-of-configuration
1513 """
1514 request = self.build_request(
1515 method=method,
1516 url=url,
(...)
1525 extensions=extensions,
1526 )
-> 1527 return await self.send(request, auth=auth, follow_redirects=follow_redirects)
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1614, in AsyncClient.send(self, request, stream, auth, follow_redirects)
1606 follow_redirects = (
1607 self.follow_redirects
1608 if isinstance(follow_redirects, UseClientDefault)
1609 else follow_redirects
1610 )
1612 auth = self._build_request_auth(request, auth)
-> 1614 response = await self._send_handling_auth(
1615 request,
1616 auth=auth,
1617 follow_redirects=follow_redirects,
1618 history=[],
1619 )
1620 try:
1621 if not stream:
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1642, in AsyncClient._send_handling_auth(self, request, auth, follow_redirects, history)
1639 request = await auth_flow.__anext__()
1641 while True:
-> 1642 response = await self._send_handling_redirects(
1643 request,
1644 follow_redirects=follow_redirects,
1645 history=history,
1646 )
1647 try:
1648 try:
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1679, in AsyncClient._send_handling_redirects(self, request, follow_redirects, history)
1676 for hook in self._event_hooks["request"]:
1677 await hook(request)
-> 1679 response = await self._send_single_request(request)
1680 try:
1681 for hook in self._event_hooks["response"]:
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx/_client.py:1716, in AsyncClient._send_single_request(self, request)
1711 raise RuntimeError(
1712 "Attempted to send an sync request with an AsyncClient instance."
1713 )
1715 with request_context(request=request):
-> 1716 response = await transport.handle_async_request(request)
1718 assert isinstance(response.stream, AsyncByteStream)
1719 response.request = request
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx_cache/transport.py:118, in AsyncCacheControlTransport.handle_async_request(self, request)
116 if self.controller.is_request_cacheable(request):
117 logger.debug(f"Checking cache for: {request}")
--> 118 cached_response = await self.cache.aget(request)
119 if cached_response is not None:
120 logger.debug(f"Found cached response for: {request}")
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx_cache/cache/file.py:66, in FileCache.aget(self, request)
64 async with RWLock().reader:
65 cached = await filepath.read_bytes()
---> 66 return self.serializer.loads(request=request, cached=cached)
67 return None
File ~/miniconda3/envs/dev/lib/python3.9/site-packages/httpx_cache/serializer/common.py:172, in MsgPackSerializer.loads(self, cached, request)
168 def loads( # type: ignore
169 self, *, cached: bytes, request: tp.Optional[httpx.Request] = None
170 ) -> httpx.Response:
171 """Load an httpx.Response from a msgapck bytes."""
--> 172 return super().loads(cached=msgpack.loads(cached, raw=False), request=request)
File msgpack/_unpacker.pyx:201, in msgpack._cmsgpack.unpackb()
ExtraData: unpack(b) received extra data.