Reputation: 10138
This is advanced question but looks simple - see code (one global variable which increment by each http handler call).
some_global_var = 0
class DebugCgiHandler(webapp.RequestHandler):
def get(self):
some_global_var++
self.response.out.write(some_global_var)
Q1. The question is what will happen in Python 2.5 and Python 2.7 in single thread mode if variable is set to 1 and concurrent call happen with another request so variable will be incremented in second request before self.response.out.write(some_global_var)
. What will be displayed in 1st request and what in 2nd?
Q2. The second question is what will happen in the same situation if Python 2.7 will be run with multithread mode?
Q3. How to avoid variables changes during whole request processing to keep same value til the end protected from other threads?
Upvotes: 0
Views: 478
Reputation: 15143
Q1: In single thread mode you won't have a concurrent request call come in so you don't have to worry about the thread safety.
Q2: With multithreaded on, you may get unpredictable behavior as the different threads could potentially increment some_global_var and display them at different times. For example, if the timing is happens in a particular way, some_global_var might only get incremented once.
Q3: Use threading.Lock()
Now I haven't answered what results you'll get, because with App Engine, this question is broken as it stands. some_global_var will be "global" in a particular instance, but you have no control over what instances your requests go to. So even in a single threaded environment, your two requests may hit two separate instances, in which case you'll see 1 for both requests. Or they may hit the same instance and you'll see 2. Your threading issues will apply on top of that.
Upvotes: 4