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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:34:46+00:00 2026-06-10T21:34:46+00:00

I’m dipping my toes into Python threading. I’ve created a supplier thread that returns

  • 0

I’m dipping my toes into Python threading. I’ve created a supplier thread that returns me character/line data from a *nix (serial) /dev via a Queue.

As an exercise, I would like to consume the data from the queue one line at a time (using ‘\n’ as the line terminator).

My current (simplistic) solution is to put() only 1 character at a time into the queue, so the consumer will only get() one character at a time. (Is this a safe assumption?) This approach currently allows me to do the following:

...
return_buffer = []
while True:
    rcv_data = queue.get(block=True)
    return_buffer.append(rcv_data)        
    if rcv_data == "\n":
        return return_buffer

This seems to be working, but I can definitely cause it to fail when I put() 2 characters at a time.

I would like to make the receive logic more generic and able to handle multi-character put()s.

My next approach would be to rcv_data.partition(“\n”), putting the “remainder” in yet another buffer/list, but that will require juggling the temporary buffer alongside the queue.
(I guess another approach would be to only put() one line at a time, but where’s the fun in that?)

Is there a more elegant way to read from a queue one line at a time?

  • 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-10T21:34:47+00:00Added an answer on June 10, 2026 at 9:34 pm

    This may be a good use for a generator. It will pick up exactly where it left off after yield, so reduces the amount of storage and buffer swapping you need (I cannot speak to its performance).

    def getLineGenerator(queue, splitOn):
        return_buffer = []
        while True:
            rcv_data = queue.get(block=True) # We can pull any number of characters here.
            for c in rcv_data:
                return_buffer.append(c)
                if c == splitOn:
                    yield return_buffer
                    return_buffer = []
    
    
    gen = getLineGenerator(myQueue, "\n")
    for line in gen:
        print line.strip()
    

    Edit:

    Once J.F. Sebastian pointed out that the line separator could be multi-character I had to solve that case as well. I also used StringIO from jdi’s answer. Again I cannot speak to the efficiency, but I believe it is correct in all cases (at least the ones I could think of). This is untested, so would probably need some tweaks to actually run. Thanks to J.F. Sebastian and jdi for their answers which ultimately lead to this one.

    def getlines(chunks, splitOn="\n"):
        r_buffer = StringIO()
        for chunk in chunks
            r_buffer.write(chunk)
            pos = r_buffer.getvalue().find(splitOn) # can't use rfind see the next comment
            while pos != -1: # A single chunk may have more than one separator
                line = r_buffer.getvalue()[:pos + len(splitOn)]
                yield line
                rest = r_buffer.getvalue().split(splitOn, 1)[1]
                r_buffer.seek(0)
                r_buffer.truncate()
                r_buffer.write(rest)
                pos = rest.find(splitOn) # rest and r_buffer are equivalent at this point. Use rest to avoid an extra call to getvalue
    
        line = r_buffer.getvalue();
        r_buffer.close() # just for completeness
        yield line # whatever is left over.
    
    for line in getlines(iter(queue.get, None)): # break on queue.put(None)
        process(line)
    
    • 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
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.