В коде основной процесс запускает новый процесс, который получает ссылку на очередь и использует Queue.put (), чтобы поместить целое число в очередь основного процесса. Я вижу, что процесс-производитель завершается во время вызова .put(), никаких исключений не возникает.
Любые идеи по этому поводу почему .put() убивает процесс? Это работает локально (macOS), но не работает с базовым образом Python для контейнера. Версия Python — 3.9.16.
import multiprocessing as mp
import time
import traceback
from typing import Any, Optional
import psutil
base_file = "logs.txt"
def main() -> None:
queue: Any = mp.Queue()
print("Queue created")
print("Starting producer process")
p = mp.get_context("spawn").Process(target=producer, args=(queue,), daemon=True)
p.start()
print(f"Main: producer started: {p.pid}")
alive = True
while alive:
alive = p.is_alive()
print(f"Ha ha ha staying alive, producer: {p.is_alive()}")
time.sleep(1)
print("Every process is dead
def producer(q: mp.Queue) -> None:
with open(f"producer.{base_file}", "w") as f:
print("Producer: started", file=f, flush=True)
current_value: int = 0
while True:
print(f"Producer: Adding value {current_value} to queue", file=f, flush=True)
try:
q.put(current_value, block=False)
except BaseException as e:
print(f"Producer: exception: {e}", file=f, flush=True)
print(f"{traceback.format_exc()}", file=f, flush=True)
raise e
print(f"Producer: Value {current_value} added to queue", file=f, flush=True)
print("Producer: Sleeping for 1 second", file=f, flush=True)
time.sleep(1)
current_value += 1
if __name__ == "__main__":
main()
FROM python:3.9.16
RUN apt-get update && apt-get install -y gettext git mime-support && apt-get clean
RUN python3 -m pip install psutil
COPY ./multiprocessing_e2e.py /src/multiprocessing_e2e.py
WORKDIR /src
CMD ["python", "-u", "multiprocessing_e2e.py"]
Подробнее здесь: https://stackoverflow.com/questions/786 ... -container