2

I am trying out basic server program which listens on port and for each incoming request, it spawns new daemon thread and continues execution. Earlier similar code used to work, now this thing is not working.

server.py

import multiprocessing as mu
from time import sleep
from random import random
import socket

def worker():
    while(True):
        print("Inside worker....")
        sleep(2)

socketObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ("127.0.0.1", 8080)
socketObj.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
socketObj.bind(server_address)
socketObj.listen(10)

while(True):
    print("Listening...")
    data_socket, client_address = socketObj.accept()
    a = data_socket.recv(9000)
    p = mu.Process(target=worker, args=())
    p.daemon = True
    p.start()

client.py

import socket

s = "dummydata"

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect(('127.0.0.1', 8080))
socket.send(str.encode(s)) 

I first ran server and then client. Somehow, it kept printing Listening... for each request (in fact it sometimes printed it twice) and never printed Inside worker.... Why the daemon thread is never spawned? Is it behaving same on your machine? If yes why?

MsA
  • 2,599
  • 3
  • 22
  • 47

1 Answers1

0

I tested your exact same code in my machine, in your exact same order, and it looks fine for me. I first ran server.py and then client.py, and got the expected answer.

Please ensure that you are running both scripts from different terminals, and that your used ports are being released after use, as ports can keep busy if you don't end/kill the processes correctly (fuser command works if in Linux, look at this answer). Thus, trying with another port number should work.

In the link below you can see a picture of my server terminal before and after I launched the client script for the first time.

Terminal Results

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Sebas Arango
  • 301
  • 2
  • 4
  • ohkay dont know whats going wrong on my windows machine. Just moved the same to Ubuntu and it started working :\ – MsA Nov 05 '18 at 11:37