On Sunday 19 January 2014 11:52:40 Łukasz Rekucki wrote:
> On 19 January 2014 09:12, Shai Berger <s...@platonix.com> wrote:
> > On Friday 17 January 2014 01:19:29 Michael Manfre wrote:
> >> In an effort to make Django a bit more explicit with regards to closing
> >> cursors when they are no longer needed, I have created ticket #21751 [1]
> >> with a pull request[2].
> >> 
> >> Most of the changes are straight forward and change the usage of a
> >> cursor so that it uses a context manager or closes the cursor in
> >> 'finally'.
> >> 
> >> Example:
> >> 
> >> # old pattern
> >> connection.cursor().execute(...)
> >> 
> >> # changes to
> >> 
> >> with connection.cursor() as cursor:
> >>    cursor.execute(...)
> > 
> > I think this is suboptimal, API-wise. Python destroys temporaries soon
> > enough.
> 
> You mean CPython, right? Considering that a DB cursor can also
> allocate resources in the DB itself, I think it's not a good idea to
> rely on GC here. Unfortunately, the pull request in question won't
> help in many cases due to the way how try: finally: works in
> generators[1].
> 
Yep, I had CPython's behavior in mind. Didn't realize how different the others 
were in this regard.

Still, spreading with-blocks all over the code for this looks very ugly to me. 
I'd rather add an execute_single() or execute_and_close() method to the 
cursors instead. Perhaps even only as private API, as that usage is mostly 
common within Django's code.

Shai.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/201401191223.53778.shai%40platonix.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to