I am trying to write a simple REST API client-server model. On the server side, I have
transaction = 0
class MyHandler(http.server.BaseHTTPRequestHandler):
global transaction
def __init__(self, *args, **kwargs):
....
def update(self):
# do something
with self.lock:
transaction = transaction + 1
if __init__ == '__main__':
...
with socketserver.ThreadingTCPServer(('', PORT), MyHandler) as server:
...
This is a part of my code. When multiple client requests come in (for example, GET/POST), multiple threads should be created to handle requests. I need to update the transaction
for each request, but such attempt cause failure. If I want to print it, also failed.
Also, if transaction
has such issue, I think I also need to move lock
outside of MyHandler
.
I don't understand why I can't access and update global variable. I did it with my multithreading socket server. Does it have relation with http.server.BaseHTTPRequestHandler
?