Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6974495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:14:41+00:00 2026-05-27T17:14:41+00:00

I’m writing interprocess communication using localhost sockets in Python 3.2 and testing it on

  • 0

I’m writing interprocess communication using localhost sockets in Python 3.2 and testing it on Windows. Here is some test code with a server and a client, sending messages to each other. Strangely, it fails randomly with the RuntimeError error, raised in the receive function, somewhere around the 5th or 10th connection.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import socket
import pickle
import time
import logging
from multiprocessing import Process

def receive(conn):
    def ensure_receive(length):
        parts = []
        received = 0
        while received < length:
            chunk = conn.recv(length - received)
            if not chunk:
                raise RuntimeError("Connection broken")
            parts.append(chunk)
            received += len(chunk)
        return b''.join(parts)
    lengthString = ensure_receive(8)
    serialized = ensure_receive(int(lengthString))
    return pickle.loads(serialized)

def send(conn, message):
    def ensure_send(message):
        sent = 0
        while sent < len(message):
            sent += conn.send(message[sent:])
#            logging.warning("Now sending")
    serialized = pickle.dumps(message, 1)
    messageLength = len(serialized)
    ensure_send("{:8}".format(messageLength).encode('Latin-1'))
    ensure_send(serialized)

def client_function(clientLimit):
    for index in range(1, clientLimit + 1):
        print ("Client", index)
        try:
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.connect(('localhost', 12333))
            send(conn, list(range(100000)))
            message = receive(conn)
            send(conn, list(range(100)))
#            time.sleep(0.01)
            conn.shutdown(socket.SHUT_WR)
            conn.close()
        except Exception:
            logging.exception("Socket error in client")

def server_function(clientLimit):
    newSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    newSocket.bind(('localhost', 12333))
    newSocket.listen(16)
    for _ in range(clientLimit):
        (conn, address) = newSocket.accept()
        time.sleep(0.01)
        message = receive(conn)
        send(conn, list(range(10)))
        message = receive(conn)
        conn.shutdown(socket.SHUT_WR)
        conn.close()

def test(clientLimit):
    server = Process(target = server_function, args = (clientLimit,))
    server.start()

    time.sleep(1)
    client = Process(target = client_function, args = (clientLimit,))
    client.start()

    client.join()
    server.join()

if __name__ == "__main__":
    test(100)

However, there are no errors if I uncomment time.sleep(0.01) in client_function, or if I change message order a bit.

Is there a way to make it work, without putting in random waits, and allowing for arbitrary protocols?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:14:42+00:00Added an answer on May 27, 2026 at 5:14 pm

    It is because of conn.shutdown(socket.SHUT_WR) in your server_function. What you need is socket.SHUT_RD, or better yet, get rid of the shutdown() call at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.