Add week 2 materials

This commit is contained in:
2026-09-14 14:45:31 +03:00
parent e29c7eb39e
commit 73a65e0855
34 changed files with 1824 additions and 0 deletions
@@ -0,0 +1,9 @@
FROM python:3.12-slim
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "./client.py"]
@@ -0,0 +1,24 @@
import grpc
import os
import time
import queue_pb2
import queue_pb2_grpc
def request_generator():
for i in range(5):
yield queue_pb2.PushRequest(value=queue_pb2.Value(payload=i))
if __name__ == '__main__':
server_addr = os.getenv('SERVER_ADDR', 'localhost:51000')
with grpc.insecure_channel(server_addr) as channel:
stub = queue_pb2_grpc.QueueStub(channel)
stub.Push(queue_pb2.PushRequest(value=queue_pb2.Value(payload=100)))
response = stub.Pop(queue_pb2.PopRequest())
print(f'Pop returned payload={response.value.payload}, updated_at={response.value.updated_at.ToDatetime()}')
time.sleep(1)
stub.PushMany(request_generator())
for response in stub.Drain(queue_pb2.DrainRequest()):
print(f'Drain returned payload={response.value.payload}, updated_at={response.value.updated_at.ToDatetime()}')
@@ -0,0 +1,34 @@
syntax = "proto3";
package queue;
import "google/protobuf/timestamp.proto";
service Queue {
rpc Push(PushRequest) returns (PushResponse);
rpc PushMany(stream PushRequest) returns (PushResponse);
rpc Pop(PopRequest) returns (PopResponse);
rpc Drain(DrainRequest) returns (stream PopResponse);
}
message Value {
uint64 payload = 1;
optional google.protobuf.Timestamp updated_at = 2;
}
message PushRequest {
Value value = 1;
}
message PushResponse {
}
message PopRequest {
}
message PopResponse {
optional Value value = 1;
}
message DrainRequest {
}
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: queue.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import symbol_database as _symbol_database
from google.protobuf.internal import builder as _builder
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__pb2
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0bqueue.proto\x12\x05queue\x1a\x1fgoogle/protobuf/timestamp.proto\"\\\n\x05Value\x12\x0f\n\x07payload\x18\x01 \x01(\x04\x12\x33\n\nupdated_at\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.TimestampH\x00\x88\x01\x01\x42\r\n\x0b_updated_at\"*\n\x0bPushRequest\x12\x1b\n\x05value\x18\x01 \x01(\x0b\x32\x0c.queue.Value\"\x0e\n\x0cPushResponse\"\x0c\n\nPopRequest\"9\n\x0bPopResponse\x12 \n\x05value\x18\x01 \x01(\x0b\x32\x0c.queue.ValueH\x00\x88\x01\x01\x42\x08\n\x06_value\"\x0e\n\x0c\x44rainRequest2\xd1\x01\n\x05Queue\x12/\n\x04Push\x12\x12.queue.PushRequest\x1a\x13.queue.PushResponse\x12\x35\n\x08PushMany\x12\x12.queue.PushRequest\x1a\x13.queue.PushResponse(\x01\x12,\n\x03Pop\x12\x11.queue.PopRequest\x1a\x12.queue.PopResponse\x12\x32\n\x05\x44rain\x12\x13.queue.DrainRequest\x1a\x12.queue.PopResponse0\x01\x62\x06proto3')
_globals = globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'queue_pb2', _globals)
if _descriptor._USE_C_DESCRIPTORS == False:
DESCRIPTOR._options = None
_globals['_VALUE']._serialized_start=55
_globals['_VALUE']._serialized_end=147
_globals['_PUSHREQUEST']._serialized_start=149
_globals['_PUSHREQUEST']._serialized_end=191
_globals['_PUSHRESPONSE']._serialized_start=193
_globals['_PUSHRESPONSE']._serialized_end=207
_globals['_POPREQUEST']._serialized_start=209
_globals['_POPREQUEST']._serialized_end=221
_globals['_POPRESPONSE']._serialized_start=223
_globals['_POPRESPONSE']._serialized_end=280
_globals['_DRAINREQUEST']._serialized_start=282
_globals['_DRAINREQUEST']._serialized_end=296
_globals['_QUEUE']._serialized_start=299
_globals['_QUEUE']._serialized_end=508
# @@protoc_insertion_point(module_scope)
@@ -0,0 +1,38 @@
from google.protobuf import timestamp_pb2 as _timestamp_pb2
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from typing import ClassVar as _ClassVar, Mapping as _Mapping, Optional as _Optional, Union as _Union
DESCRIPTOR: _descriptor.FileDescriptor
class Value(_message.Message):
__slots__ = ["payload", "updated_at"]
PAYLOAD_FIELD_NUMBER: _ClassVar[int]
UPDATED_AT_FIELD_NUMBER: _ClassVar[int]
payload: int
updated_at: _timestamp_pb2.Timestamp
def __init__(self, payload: _Optional[int] = ..., updated_at: _Optional[_Union[_timestamp_pb2.Timestamp, _Mapping]] = ...) -> None: ...
class PushRequest(_message.Message):
__slots__ = ["value"]
VALUE_FIELD_NUMBER: _ClassVar[int]
value: Value
def __init__(self, value: _Optional[_Union[Value, _Mapping]] = ...) -> None: ...
class PushResponse(_message.Message):
__slots__ = []
def __init__(self) -> None: ...
class PopRequest(_message.Message):
__slots__ = []
def __init__(self) -> None: ...
class PopResponse(_message.Message):
__slots__ = ["value"]
VALUE_FIELD_NUMBER: _ClassVar[int]
value: Value
def __init__(self, value: _Optional[_Union[Value, _Mapping]] = ...) -> None: ...
class DrainRequest(_message.Message):
__slots__ = []
def __init__(self) -> None: ...
@@ -0,0 +1,165 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import queue_pb2 as queue__pb2
class QueueStub(object):
"""Missing associated documentation comment in .proto file."""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.Push = channel.unary_unary(
'/queue.Queue/Push',
request_serializer=queue__pb2.PushRequest.SerializeToString,
response_deserializer=queue__pb2.PushResponse.FromString,
)
self.PushMany = channel.stream_unary(
'/queue.Queue/PushMany',
request_serializer=queue__pb2.PushRequest.SerializeToString,
response_deserializer=queue__pb2.PushResponse.FromString,
)
self.Pop = channel.unary_unary(
'/queue.Queue/Pop',
request_serializer=queue__pb2.PopRequest.SerializeToString,
response_deserializer=queue__pb2.PopResponse.FromString,
)
self.Drain = channel.unary_stream(
'/queue.Queue/Drain',
request_serializer=queue__pb2.DrainRequest.SerializeToString,
response_deserializer=queue__pb2.PopResponse.FromString,
)
class QueueServicer(object):
"""Missing associated documentation comment in .proto file."""
def Push(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def PushMany(self, request_iterator, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def Pop(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def Drain(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_QueueServicer_to_server(servicer, server):
rpc_method_handlers = {
'Push': grpc.unary_unary_rpc_method_handler(
servicer.Push,
request_deserializer=queue__pb2.PushRequest.FromString,
response_serializer=queue__pb2.PushResponse.SerializeToString,
),
'PushMany': grpc.stream_unary_rpc_method_handler(
servicer.PushMany,
request_deserializer=queue__pb2.PushRequest.FromString,
response_serializer=queue__pb2.PushResponse.SerializeToString,
),
'Pop': grpc.unary_unary_rpc_method_handler(
servicer.Pop,
request_deserializer=queue__pb2.PopRequest.FromString,
response_serializer=queue__pb2.PopResponse.SerializeToString,
),
'Drain': grpc.unary_stream_rpc_method_handler(
servicer.Drain,
request_deserializer=queue__pb2.DrainRequest.FromString,
response_serializer=queue__pb2.PopResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'queue.Queue', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
# This class is part of an EXPERIMENTAL API.
class Queue(object):
"""Missing associated documentation comment in .proto file."""
@staticmethod
def Push(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/queue.Queue/Push',
queue__pb2.PushRequest.SerializeToString,
queue__pb2.PushResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
@staticmethod
def PushMany(request_iterator,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.stream_unary(request_iterator, target, '/queue.Queue/PushMany',
queue__pb2.PushRequest.SerializeToString,
queue__pb2.PushResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
@staticmethod
def Pop(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/queue.Queue/Pop',
queue__pb2.PopRequest.SerializeToString,
queue__pb2.PopResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
@staticmethod
def Drain(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_stream(request, target, '/queue.Queue/Drain',
queue__pb2.DrainRequest.SerializeToString,
queue__pb2.PopResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
@@ -0,0 +1,3 @@
grpcio==1.66.1
grpcio-tools==1.66.1
protobuf==5.28.1