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
Result of query_entities
of TableClient
cannot be returned in with
keyword successfully
#25640
Comments
Label prediction was below confidence level |
Thanks @matt-mi! The context manager of the There are two ways you could resolve this. def get_cluster():
cluster_name_filter = "RowKey eq 'cluster4'"
with TableClient(endpoint=sta_endpoint, table_name=table_name, credential=credential) as table_client:
return list(table_client.query_entities(query_filter=cluster_name_filter))
results = get_cluster()
for i in results:
print(i['RowKey']) However the above solution will lose the incremental nature of the query, and if there's a lot of data to return this could be very slow. So the second option is to turn the entire function into a generator that will yield the results and therefore both iteratively download the data, but also not close the connection until the iteration is complete: def get_cluster():
cluster_name_filter = "RowKey eq 'cluster4'"
with TableClient(endpoint=sta_endpoint, table_name=table_name, credential=credential) as table_client:
for result in table_client.query_entities(query_filter=cluster_name_filter):
yield result
results = get_cluster()
for i in results:
print(i['RowKey']) Hopefully one of these two options will fix your current error, and we will look at improving the failure experience for when the connection is closed on an orphaned iterator. @iscai-msft, @xiangyan99 - do you know if we have tests for this scenario in the azure-core tests for |
Thanks @annatisch for your prompt response on this! There's one thing I'm kind of confused about, which is for example if I was trying to manually create and close the def get_cluster():
cluster_name_filter = "RowKey eq 'cluster4'"
table_client = TableClient(endpoint=sta_endpoint, table_name=table_name, credential=credential)
results = table_client.query_entities(query_filter=cluster_name_filter)
table_client.close()
return results
for i in get_cluster():
print(i['RowKey']) I'm not quite sure what's the difference and forgive me about my limited knowledge in python ... |
Thanks @matt-mi - that is.... very interesting indeed. |
Ah - I have discovered the reason - and there's possibly something we could look at fixing as well... though might need more discussion. In this case, using the context manager - i.e. the However in the second example, simply creating the TableClient does not open the connection - this is done lazily on the first request. So the @xiangyan99 - it looks like we have two questions for core here:
|
Thank you for your detailed explanation Anna! I thought the connection was already initiated when a So in this case if I don't want to use |
Yes @matt-mi - that is correct, so the following (adding def get_cluster():
cluster_name_filter = "RowKey eq 'cluster4'"
table_client = TableClient(endpoint=sta_endpoint, table_name=table_name, credential=credential)
results = list(table_client.query_entities(query_filter=cluster_name_filter))
table_client.close()
return results
for i in get_cluster():
print(i['RowKey']) Edit: |
Awesome, I will try to adapt |
You're very welcome @matt-mi! |
I found this issue after I got confused by your documentation.
As a user, I now don't know which way would be correct (with with or without with), and whether there actually is some stuff that needs to be cleaned up - or if the exiting the context has no effect in general. Also, it would be nice to know if above changes when switching from sync to async. Having to use with statements or not can have quite an impact on the design of our application. IMHO: If there is stuff that needs to be cleaned up, then one should have to use the with statement - otherwise users will experience weird bugs, unsubmitted requests or whatever that are hard to debug. I would "simply" prevent usage of the clients without a context. Same as with |
Describe the bug
Result of
query_entities
ofTableClient
cannot be returned inwith
keywordTo Reproduce
Steps to reproduce the behavior:
cluster4
underRowKey
.Expected behavior
Results can be returned successfully from
with
keywordScreenshots
If applicable, add screenshots to help explain your problem.
Additional context
If I'm not using
with
to automatically create and closeTableClient
, results can be returned and lopped successfully.The text was updated successfully, but these errors were encountered: