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 9184461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:03:26+00:00 2026-06-17T19:03:26+00:00

I am connecting to a server that will send me streaming data that needs

  • 0

I am connecting to a server that will send me streaming data that needs to be processed on a per-line basis. So I have to parse out the individual lines, then process each line. The following code appears to work just fine, but I am wondering if there are any standard design patterns for doing this type of thing. Or is this the way to go?

Does the Queue introduce any serious overhead? I need it to be as fast and efficient as possible, which is also why I strayed away from libraries like twisted.

import socket, multiprocessing

def receive_proc(s, q):
    data = ''
    while True:
        data += s.recv(4096)
        if '\n' in data:
            lines = data.split('\n')[:-1]
            for line in lines:
                if len(line) > 0:
                    q.put(line)
                    data = data.replace(line+'\n', '', 1)

q = multiprocessing.Queue()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 1234))

p = multiprocessing.Process(target=receive_proc, args=(s,q))
p.start()

while True:
    line = q.get()

    # do your processing here
  • 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-17T19:03:27+00:00Added an answer on June 17, 2026 at 7:03 pm

    There are certainly valid reasons for wanting to stay away from things like twisted, but I don’t think efficiency is among them – I suspect they’re more likely to be optimised in the right ways. Performance is a tricky beast and often the bottlenecks aren’t really where you thought, which is why you need to profile before you can properly optimise. For example, frameworks may have made the effort to push more of their code out into C extensions which will definitely help performance. If performance is your key motivator, third party stuff is probably the safer option. Also, there’s a big argument for using code that other people have tested and tweaked for all sorts of different use cases and environments – if you end up reinventing too much of the wheel, there’s always the risk it might be missing a few spokes.

    However, what you need to do seems quite simple so the overhead of installing and learning a framework, and also adding another runtime dependency to your code, may not be justified. Also, if you’re primarily IO-bound then burning a bit of extra CPU doing your processing isn’t going to make much difference anyway. I’ve certainly avoided things like twisted at times in the past simply because I knew it would be faster (in terms of my time) to write it myself and performance would be “good enough”. I’ve always found twisted’s system of callbacks makes debugging a little bit tricky – getting access to error messages can be a little fraught, for example. It’s by no means impossible and many people use it very successfully, but personally I found it too “fiddly” to be justified for simple tasks.

    I think your idea of splitting receiving and processing into their own processes might be a false economy in this case – receiving data from a socket is extremely fast, and if you’re doing significant amounts of processing in pure Python that’s likely to be the dominant performance factor. However, I can’t say for sure without knowing what processing you’re doing. If it’s going to be time-consuming and/or CPU-intensive, and you can process each line independent of previous lines, then it’s probably reasonable but you’d likely want to farm the processing out to a whole set of worker processes. This is pretty easy based on your existing code – just make the main process the receiver instead of the “slave” and create a pool of workers which all share a Queue. Each worker goes through a loop picking the next item and producing the result. It doesn’t matter how long each takes, they just get the next item as it becomes available (and Queue will handle that for you).

    If, however, your processing loop is also primarily IO-bound (e.g. writing to a file) then you might find a single process is actually better than the overhead of pushing everything down a pipe. This depends on many factors including your CPU architecture (some systems make transfers between CPU cores more expensive than others), but ultimately you don’t want to use multiple processes unless you’re pretty confident it’s going to give you a performance win.

    Anyway, if the loop is IO-bound you might find a single process with non-blocking IO is the way to go. You can use Python’s select module to do this yourself, or you may find it cleaner using a library like eventlet or gevent.

    Unrelated aside – your method of stripping the start off the buffer is quite inefficient – you don’t need to use replace() you can just use your existing split(), like this:

    while True:
        data += s.recv(4096)
        if '\n' in data:
            lines = data.split('\n')
            for line in lines[:-1]:
                if len(line) > 0:
                    q.put(line)
            data = lines[-1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Client program by tcp connection that send data to a server.
I have created a basic Client Server that will send image files in a
I have some Crystal Reports connecting to a Sql Server db that I would
My target: Create node.js server and client applications that will send antyhing (event, string,
I am writing a game server that requires connecting to a MySQL database server
I have a server that connects to multiple clients using TCP/IP connections, using C
I have some Lotus Notes clients that looses server connection. They suddenly get server
I have a server that communicates to a lot of devices (>1000). Each connection
I have an Apache SSL connection available with a C# server that listens to
I currently have an app that requires connection to a server in multiple activities.

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.