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

  • Home
  • SEARCH
  • 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 8179829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:05:05+00:00 2026-06-07T00:05:05+00:00

I have a machine control system in Python that currently looks roughly like this

  • 0

I have a machine control system in Python that currently looks roughly like this

goal = GoalState()
while True:
    current = get_current_state()
    move_toward_goal(current,goal)

Now, I’m trying to add in the ability to control the machine over the network. The code I want to write would be something like this:

goal = GoalState()
while True:
    if message_over_network():
        goal = new_goal_from_message()
    current = get_current_state()
    move_toward_goal(current,goal)

What would be the simplest and most Pythonic way of adding this sort of networking capability into my application? Sockets could work, thought they don’t particularly feel Pythonic. I’ve looked at XMLRPC and Twisted, but both seemed like they would require major revisions to the code. I also looked at ØMQ, but it felt like I was adding an external dependency that didn’t offer anything that I didn’t already have with sockets.

I’m not opposed to using any of the systems that I’ve addressed above, as what I believe to be failings are probably misunderstandings on my part. I’m simply curious as to the idiomatic way of handling this simple, common issue.

  • 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-06-07T00:05:07+00:00Added an answer on June 7, 2026 at 12:05 am

    The are at least two issues you need to decide on:

    1. How to exchange messages?
    2. In what format?

    Regarding 1. TCP sockets are the lowest level and you would need to deal with low level things like recognizing messages boundaries. Also, TCP connection gives you reliable delivery but only as long as the connection is not reset (due to for example a temporary network failure). If you want your application to gracefully recover when a TCP connection resets, you need to implement some form of messages acknowledgements to keep track what needs to be resend over the new connection. OMQ gives you higher level of abstraction than plain TCP connection. You don’t need to deal with a stream of bytes but with whole messages. It still does not give you reliable delivery, messages can get lost, but it gives several communication patterns that can be used to ensure reliable delivery. 0MQ is also highly performant, IMO it is a good choice.

    Regarding 2, if interoperability with other languages is not needed, Pickle is a very convenient and Pythonic choice. If interoperability is needed, you can consider JSON, or, if performance is an issue, binary format, such as Google protocol buffers. This last choice would require the most work (you’ll need to define messages formats in .idl files) this would definitely not feel Pythonic.

    Take a look how exchange of messages (any serializable Python object) over a plain socket can look like:

    def send(sockfd, message):
        string_message = cPickle.dumps(message)
        write_int(sockfd, len(string_message))
        write(sockfd, string_message)
    
    def write_int(sockfd, integer):
        integer_buf = struct.pack('>i', integer)       
        write(sockfd, integer_buf)
    
    def write(sockfd, data):
        data_len = len(data)
        offset = 0
        while offset != data_len:
            offset += sockfd.send(data[offset:])
    

    Not bad, but as you can see having to deal with serialization of a message length is quite low level.

    And to receive such message:

    def receive(self):
        message_size = read_int(self.sockfd)
        if message_size == None:
            return None
        data = read(self.sockfd, message_size)
        if data == None:
            return None
        message = cPickle.loads(data)
        return message
    
    def read_int(sockfd):
        int_size = struct.calcsize('>i')
        intbuf = read(sockfd, int_size)
        if intbuf == None:
            return None
        return struct.unpack('>i', intbuf)[0]
    
    def read(sockfd, size):
        data = ""
        while len(data) != size:
            newdata = sockfd.recv(size - len(data))
            if len(newdata) == 0:
               return None
            data = data + newdata
        return data
    

    But this does not gracefully deal with errors (no attempt to determine which messages were delivered successfully).

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

Sidebar

Related Questions

I have a machine that is running Ubuntu Hardy, which provides its own RubyGems
I currently have a machine with an Opteron 275 (2.2Ghz), which is a dual
Currently, I have a machine on which I am working in Eclipse, it says
I'm in a team that all members have VS2008 installed on their machine, we
I have a Windows user control that reads events from the application log of
I'm looking for a source control system that I can use for personal projects.
The target machine running the python application will have three network interfaces available to
I have the following problem: I want to simulate some control engineering system. As
i have struggled to implement git as my version control system on numerous occasions...
I currently have existing Intellij IDEA projects that are tied to the SVN repository.

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.