Strange behavior of python socket in docker container

一曲冷凌霜 提交于 2020-05-14 05:51:27

问题


I observed a behavior of my python code running in a docker container I don't understand. Maybe someone can explain me the behavior and can me tell the difference between python socket on Ubuntu 18.04 and the docker image python:3.6

If I start my UDP socket at my host the server thread unblock on every received datagram and print the bytes out. That is the behavior I expect.

If I start the same code in docker container, there is no unblocking and no output to stdout. If a stop the server with ctrl+c the output is printed before the interrupt exception.

UPDATE

  • Add a example for a thread printing out to stdout without any problems. See I/O Test.
  • Add code to show the the ininstantiating of the objects and the start() call.
  • Add Docker run commands. Start the container and print out stdout.
  • Remove unnecessary print() output.

Thanks and enjoy my creepy code!

Dockerfile: Server

FROM python:3.6

COPY . /workdir
EXPOSE 8888
WORKDIR /workdir
CMD ["./main.py"]

Dockerfile: I/O Test

FROM python:3.6

COPY . /workdir
EXPOSE 8888
WORKDIR /workdir
CMD ["./iotest.py"]

I/O test (same as UDPServer but without blocking socket) *

import threading

    class iotest(threading.Thread):

        def __init__(self):
            threading.Thread.__init__(self)

        def run(self):
            print("Server Thread started!")
            try:
                while True:
                    print("Test i/o")
            except Exception as e:
                print("Exception catched")
                print(e)
            finally:
                print("Thread exited")

Server:

import threading
import socket


class UDPServer(threading.Thread):
    PORT = 8888
    HOST = '0.0.0.0'

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("Server Thread started!")
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.bind((UDPServer.HOST, UDPServer.PORT))
            while True:
                msg = s.recv(4)
                print("Message received")
        except Exception as e:
            print("Exception catched")
            print(e)
        finally:
            print("Thread exited, Socket closed")
            s.close()

I/O test (same as UDPServer but without blocking socket) *

import threading

    class iotest(threading.Thread):

        def __init__(self):
            threading.Thread.__init__(self)

        def run(self):
            print("Server Thread started!")
            try:
                while True:
                    print("Test i/o")
            except Exception as e:
                print("Exception catched")
                print(e)
            finally:
                print("Thread exited")

Client:

import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('0.0.0.0', 8888))
count = 0
errors = 0
for i in range(10):
    isTime = time.time()
    msg = struct.pack('!f', isTime, )
    time.sleep(0.5)
    try:
        size_date_sent = s.send(msg)
        count += 1
    except Exception as identifier:
        errors += 1

print(str(count) + " MSG sent.")
print(str(errors) + " errors.")
s.close()

main.py:

import UDPServer

server = UDPServer.UDPServer()
server.start()

iotest.py

import iotest

thr = iotest.iotest()
thr.start

Build and run

docker build -t pythontest .
docker run -p 8888:8888/udp pythontest

来源:https://stackoverflow.com/questions/54990469/strange-behavior-of-python-socket-in-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!