Basi c Блокировка на HTTP-сервере с Python - PullRequest
0 голосов
/ 23 января 2020

Я использую базовый c HTTP-сервер, который опрашивает базу данных через PostGRESQL. Для обработки нескольких запросов мне нужно создать поток на сервере для каждого из них, и в настоящее время я делаю это.

class MyServer(ThreadingMixIn, HTTPServer):
def __init__(self, server_address, handler_class):
    super().__init__(server_address=server_address, RequestHandlerClass=handler_class)
    self.KEEP_ALIVE = True

def run(self):
    while self.KEEP_ALIVE:
        self.handle_request()

, и, например, мой метод do_DELETE выполняется следующим образом:

    def do_DELETE(self):
    # Get ID from the request path
    config_id = self.path[2:]

    # Connect to DB
    connection_db, cursor = self.connect_to_DB()

    with connection_db:

        # Check if the config to delete exists
        cursor.execute('SELECT * FROM configuration WHERE id=%s', (config_id, ))
        res = cursor.fetchall()

        # If the config to delete exists, the operation is done. Otherwise an error message is sent back
        if len(res) != 0:
            cursor.execute('DELETE FROM configuration WHERE id=%s', (config_id,))
            response_code = 200
            answer = OPERATION_SUCCESSFUL
        else:
            # Send error message back
            response_code = 400
            answer = NO_SUCH_ID_ERROR
        content_type = 'text/plain'
        self.reply(response_code=response_code, content_type=content_type, answer=answer)

Я хотел бы сделать так, чтобы в методе do_DELETE часть с двумя запросами к БД была atomi c, т.е. она должна блокировать ресурс, видимый всеми потоками .. но я понятия не имею, как это сделать. Вы можете мне помочь?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...