Example:
Find the sum of a column 1-1000
Single Thread:
1+2+3+4....+998+999+1000
4 Threads:
A= 1+2+3+4...248+249+250
B = 251+252+253+254...498+499+500
C = 501+502+503+504...748+749+750
D = 751+752+753+754...998+999+1000
Return A+B+C+D
Not Thread Safe:
Replace all entities with Value -1 with 0
Sum column
Not Thread Safe:
New A = Old A + 3
New B = New A + Old B
Can be multithreaded for part of the operation:
Fetch 4 URLs, Store returned data, do formula, display result.
You can multithread the Fetch, and the store, but the Do formula and display
result has to happen after.
Hi -
Cheers!
Greg.
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to
google-appengi...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.
- Don't rely on global state, because multiple of your functions might be running simultaneously
This usually isn't very hard to achieve - just pass parameters instead of modifying globals. The places where it can get tricky are where you really *want* to use global state, such as for an in-memory cache. Usually the language provides some primitives to ensure that only one thread at a time is updating the cache. It appears that python gives you thread-safety for a lot of cases:
http://effbot.org/zone/thread-synchronization.htm
townCache = {}
def getTown(id):
if not id in townCache:
townCache[id] = TownModel.get_by_id(id)
return townCache[id]
Is this thread safe? I think it is, because the worst that happens is the assignment happens redundantly with the same data.
Random other question: Why don't I have to say "global townCache" at the top of that function?
Thanks for that clarification. I'm sure there's a reason for this asymmetry (must declare globals to write them, but not to read them), but it's really weird.
On Jul 1, 2011, at 6:12 PM, Geoffrey Spear wrote:
>
>
> On Jun 29, 1:40 pm, Joshua Smith <JoshuaESm...@charter.net> wrote:
>> I have this code in one of my apps:
>>
>> townCache = {}
>> def getTown(id):
>> if not id in townCache:
>> townCache[id] = TownModel.get_by_id(id)
>> return townCache[id]
>>
>> Is this thread safe? I think it is, because the worst that happens is the assignment happens redundantly with the same data.
>>
>> Random other question: Why don't I have to say "global townCache" at the top of that function?
>
> You can't *assign* to a global variable in another scope without the
> global keyword; however, townCache is the global name here, not
> townCache[id].
>
> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.