Skip to content

fix: run blocking call in threadpool #780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2021
Merged

fix: run blocking call in threadpool #780

merged 2 commits into from
Jul 2, 2021

Conversation

kumaraditya303
Copy link
Contributor

No description provided.

@@ -298,8 +299,7 @@ async def _pause(self) -> None:
async def storage_state(self, path: Union[str, Path] = None) -> StorageState:
result = await self._channel.send_return_as_dict("storageState")
if path:
with open(path, "w") as f:
json.dump(result, f)
await async_writefile(path, "w", json.dumps(result))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was already leaving this comment, should be async_write_file(page, json.dumps(result))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay but how would you write in binary with this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OR you mean we should have two functions for writing one for str and one for bytes ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async_write_file would accept Union[str, bytes] and will pick the right mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Thanks for confirming

@@ -214,5 +215,4 @@ def normalize_context_params(is_sync: bool, params: Dict) -> None:
if "storageState" in params:
storageState = params["storageState"]
if not isinstance(storageState, dict):
with open(storageState, "r") as f:
params["storageState"] = json.load(f)
params["storageState"] = json.loads(await async_read_text(storageState))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(await async_read_file()).decode()

@@ -241,8 +242,7 @@ async def screenshot(
decoded_binary = base64.b64decode(encoded_binary)
if path:
make_dirs_for_file(path)
with open(path, "wb") as fd:
fd.write(decoded_binary)
await async_writefile(path, "wb", decoded_binary)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

file_payloads.append(
{
"name": os.path.basename(item),
"buffer": base64.b64encode(await async_read_binary(item)).decode(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async_read_file

await loop.run_in_executor(None, inner)


async def async_read_binary(file: Union[str, Path]) -> bytes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async_read_file



async def async_read_text(file: Union[str, Path]) -> str:
return (await async_read_binary(file)).decode()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline this, no reason to have a separate function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@pavelfeldman pavelfeldman merged commit f9abeae into microsoft:master Jul 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants