Skip to content

Add sync multiprocess support #3972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions python/python/glide/sync/glide_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

import os
import sys
from pathlib import Path
from typing import List, Optional, Union
Expand Down Expand Up @@ -49,8 +50,16 @@ def create(cls, config: BaseClientConfiguration) -> Self:
self._init_ffi()
self.config = config
self._is_closed = False
conn_req = config._create_a_protobuf_conn_request(
cluster_mode=type(config) is GlideClusterClientConfiguration

os.register_at_fork(after_in_child=self._create_core_client)

self._create_core_client()

return self

def _create_core_client(self):
conn_req = self.config._create_a_protobuf_conn_request(
cluster_mode=type(self.config) is GlideClusterClientConfiguration
)
conn_req_bytes = conn_req.SerializeToString()
client_type = self.ffi.new(
Expand Down Expand Up @@ -83,7 +92,6 @@ def create(cls, config: BaseClientConfiguration) -> Self:
self.lib.free_connection_response(client_response_ptr)
else:
raise ClosingError("Failed to create client, response pointer is NULL.")
return self

def _init_ffi(self):
self.ffi = FFI()
Expand Down
24 changes: 24 additions & 0 deletions python/tests/sync_tests/test_sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import math
import os
import threading
import time
from datetime import date, datetime, timedelta, timezone
Expand Down Expand Up @@ -380,6 +381,29 @@ def connect_to_client():
# Clean up the main client
client.close()

@pytest.mark.parametrize("cluster_mode", [True, False])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
def test_sync_fork(self, glide_sync_client: TGlideClient):
parent_pid = os.getpid()
try:
pid = os.fork()
except OSError as e:
pytest.fail(f"Fork failed: {e}")

current_pid = os.getpid()
if current_pid != parent_pid:
# Child process
glide_sync_client.set("key", "value")
assert glide_sync_client.get("key") == "value".encode()
os._exit(0)
else:
# Parent process
glide_sync_client.set("key", "value")
assert glide_sync_client.get("key") == "value".encode()
_, status = os.waitpid(pid, 0)
if not os.WIFEXITED(status) or os.WEXITSTATUS(status) != 0:
pytest.fail(f"Child process failed with status {status}")


class TestCommands:
@pytest.mark.smoke_test
Expand Down
Loading