Skip to content

Commit

Permalink
server embedder - send bytes as content
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Aug 1, 2022
1 parent 99b1966 commit dc1693a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 5 additions & 3 deletions Orange/misc/server_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from functools import partial
from json import JSONDecodeError
from os import getenv
from typing import Any, Callable, List, Optional
from typing import Any, Callable, List, Optional, Dict, Union

from AnyQt.QtCore import QSettings
from httpx import AsyncClient, NetworkError, ReadTimeout, Response
Expand Down Expand Up @@ -306,7 +306,7 @@ async def _send_to_server(
queue.task_done()

async def _send_request(
self, client: AsyncClient, data: bytes, url: str
self, client: AsyncClient, data: Union[bytes, Dict], url: str
) -> Optional[List[float]]:
"""
This function sends a single request to the server.
Expand All @@ -331,7 +331,9 @@ async def _send_request(
"Content-Length": str(len(data)),
}
try:
response = await client.post(url, headers=headers, data=data)
# bytes are sent as content parameter and dictionary as data
kwargs = dict(content=data) if isinstance(data, bytes) else dict(data=data)
response = await client.post(url, headers=headers, **kwargs)
except ReadTimeout as ex:
log.debug("Read timeout", exc_info=True)
# it happens when server do not respond in time defined by timeout
Expand Down
3 changes: 2 additions & 1 deletion Orange/misc/tests/test_server_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ def __init__(self, content):
def make_dummy_post(response):
@staticmethod
# pylint: disable=unused-argument
async def dummy_post(url, headers, data):
async def dummy_post(url, headers, content=None, data=None):
# when sleeping some workers to still compute while other are done
# it causes that not all embeddings are computed if we do not wait all
# workers to finish
assert (content is None) ^ (data is None)
await asyncio.sleep(random() / 10)
return DummyResponse(content=response)

Expand Down

0 comments on commit dc1693a

Please sign in to comment.