Skip to content

Commit

Permalink
Merge pull request #53 from GDATASoftwareAG/python_async
Browse files Browse the repository at this point in the history
Added async to read, open file and generating hash
  • Loading branch information
doxthree authored Jun 30, 2022
2 parents 15dd9b6 + 0610160 commit c2a2231
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ python-dotenv==0.20.0
httpx[http2]==0.23.0
build==0.7.0
jwt==1.3.1
authlib==1.0.1
authlib==1.0.1
aiofiles==0.8.0
1 change: 1 addition & 0 deletions python/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ install_requires =
httpx[http2] == 0.23.0
jwt == 1.3.1
authlib == 1.0.1
aiofiles==0.8.0

[options.packages.find]
where = src
12 changes: 9 additions & 3 deletions python/src/vaas/vaas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import uuid
import asyncio
from asyncio import Future
import aiofiles
from jwt import JWT
import httpx
import websockets.client
Expand Down Expand Up @@ -147,7 +148,12 @@ async def __receive_loop(self):
async def for_buffer(self, buffer):
"""Returns the verdict for a buffer"""
start = time.time()
sha256 = hashlib.sha256(buffer).hexdigest()

loop = asyncio.get_running_loop()
sha256 = await loop.run_in_executor(
None, lambda: hashlib.sha256(buffer).hexdigest()
)

response = await self.__for_sha256(sha256)
verdict = response.get("verdict")

Expand All @@ -170,8 +176,8 @@ async def for_buffer(self, buffer):

async def for_file(self, path):
"""Returns the verdict for a file"""
with open(path, "rb") as open_file:
return await self.for_buffer(open_file.read())
async with aiofiles.open(path, mode="rb") as open_file:
return await self.for_buffer(await open_file.read())

async def __upload(self, token, upload_uri, buffer):
jwt = JWT()
Expand Down

0 comments on commit c2a2231

Please sign in to comment.