I have posted code in Stack Overflow questions and answers many times, but for some reason the post shown in the attached screenshot is not being accepted.
Note that the code correctly identified in the preview!? I've tried prepending four spaces before pasting the code, and I've tried using the editor's code widget, but nothing works.
Here's the second half of the post, as requested:
And a link to the pastie with the original Markdown.
And the original Markdown code
I am noticing a behavior around postgres transactions (the query
happens to involve postgis if that matters) that has me perplexed, and
it seems a general enough question that others might be interested.
in brief, i am caching zip codes found using a `geom__contains` query.
the following view uses both the `@transaction.atomic` and
`with transaction.atomic()` context, as i understand is best practice. note
i have some verbose timing reported because i was curious:
@transaction.atomic
def add_geo(request):
begTime = datetime.now()
begTimeStr = begTime.strftime('%y%m%d_%H%M%S')
print 'add_geo: Start=%s' % (begTimeStr)
nmissPt = 0
nullPt = Point([])
rptInterval = 10000
for i,c in enumerate(OakCrime.objects.all()):
try:
with transaction.atomic():
# pnt X is longitude, Y is latitude
pnt = Point(c.long, c.lat)
c.point = pnt
if c.point==nullPt:
nmissPt += 1
continue
zipgeo = Zip5Geo.objects.get(geom__contains=pnt)
c.zip = zipgeo.zcta5ce10
except IntegrityError,e:
print 'add_geo Integrity?! %d %s %s' % (i,c.opd_rd,e)
except Exception, e:
print 'add_geo?! %d %s %s' % (i,c.opd_rd,e)
if (i % rptInterval) == 0:
elapTime = datetime.now() - begTime
print 'add_geo: %d %s NMiss=%d' % (i,elapTime.total_seconds(),nmissPt)
return HttpResponse("You're at add_geo")
This generates the following timing log:
add_geo: Start=170330_121846
add_geo: Start=170330_121857
add_geo: 0 40.425168 NMiss=0
add_geo: 0 44.995976 NMiss=0
add_geo: 1000 59.747211 NMiss=86
add_geo: 1000 49.271699 NMiss=86
add_geo: 2000 63.778699 NMiss=162
add_geo: 2000 53.246911 NMiss=162
...
Question#1: there is significant startup time; what's going on during
that?
Question#2: *Why are there **two** reporting lines generated* each
time?! note that this includes the very first "Start=" message!?
Question#3: Why is the *first* elapsed time ~ 10 seconds *ahead of*
the second?!
Question#4: When i change the reporting interval to 10000, i now get
only one reporting line vs. two? This makes me think there must be
some per-transaction buffer or similar that i am within with n=1000
but beyond at n=10000? That seems a good thing, and the process does
seem to run much fast with n=10000; why is that?
with transaction.atomic()
" as it appears as though you've included a line break in your inline code. Hard to tell from a screenshot. Regardless, the question in the screenshot, if posted, could be closed as "too broad" as you are asking four questions in one.i
surrounded by spaces, which could be taken to be a variable name. Which brings us back to yesterday's discussion about properly capitalizing the first person singular.