pydgraph icon indicating copy to clipboard operation
pydgraph copied to clipboard

Add context manager to Txn and DgraphClientStub.

Open garylavayou opened this issue 3 years ago • 9 comments

Using Transaction with Context Manager

The Python context manager will automatically perform the "commit" action after all queries and mutations have been done and perform "discard" action to clean the transaction. When something goes wrong in the scope of context manager, "commit" will not be called, and the "discard" action will be called to drop any potential changes.

with client.begin(read_only=False, best_effort=False) as txn:
  # Do some queries or mutations here

or you can directly create a transaction from the Txn class.

with pydgraph.Txn(client, read_only=False, best_effort=False) as txn:
  # Do some queries or mutations here

client.begin() can only be used with "with-as" blocks, while pydgraph.Txn class can be directly called to instantiate a transaction object.

Use context manager to automatically clean resources

Use function call:

with pydgraph.client_stub(SERVER_ADDR) as stub:
    client = pydgraph.DgraphClient(stub)

Use class constructor:

with pydgraph.DgraphClientStub(SERVER_ADDR) as stub:
    client = pydgraph.DgraphClient(stub)

Note: client should be used inside the "with-as" block. The resources related to client will be automatically released outside the block and client is not usable anymore.

garylavayou avatar Nov 29 '22 13:11 garylavayou

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 29 '22 13:11 CLAassistant

@garylavayou Thank you for the PR. will you be able to rebase the change on the latest master?

mangalaman93 avatar May 15 '23 17:05 mangalaman93

@garylavayou Thank you for the PR. will you be able to rebase the change on the latest master?

I have synced the forked repo with the latest master. Is that enough?

garylavayou avatar May 16 '23 00:05 garylavayou

Could you also sign the CLA please before we review the code?

I have two comments/questions here:

  1. Will this API be useful to the users of pydgraph? I think so and I am getting more opinions here from my colleagues.
  2. We should add more tests trying to stress this code? Do you think you would be able to do that? I can also brainstorm with you or by myself and come up with a set of tests if you like.

mangalaman93 avatar May 17 '23 07:05 mangalaman93

@mangalaman93 It would be better if you can add tests to the code, since I currently have no stable test environment. I am also looking forward that you can make the context manager more robust and extend the function with your opinions.

garylavayou avatar May 18 '23 06:05 garylavayou

@garylavayou Thanks for responding back. I am busy with a few other things, I highly doubt if I could spend time writing tests right now. But I will keep this in mind. If you do get more time and opportunity to make changes, please go ahead and keep me posted. Appreciate your time and responding back.

mangalaman93 avatar May 18 '23 15:05 mangalaman93

This PR has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open.

github-actions[bot] avatar Jul 11 '24 01:07 github-actions[bot]

This PR has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open.

keep it open please!

garylavayou avatar Jul 11 '24 04:07 garylavayou