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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:47:47+00:00 2026-05-16T21:47:47+00:00

I have one thread that inserts into the queueStream (not shown here) and FlowController

  • 0

I have one thread that inserts into the queueStream (not shown here) and FlowController which is another thread that pops from the queue if the queue is not empty.

I verified that the data is inserted into the queue correctly with the debug code in addToQueue()

Problem is, the ‘if queueStream’ statement in FlowController always sees the queueStream as empty, and instead goes to the else statement.

I’m new to Python and I feel I’m missing some simple scoping rules of some kind. I am using the ‘global queueStream’ but that appears to not be doing anything.

Thanks for any help.

from stream import *
from textwrap import TextWrapper
import threading
import time


queueStream = []

class FlowController(threading.Thread):
    def run(self):
        global queueStream
        while True:
            if queueStream:
                print 'Handling tweet'
                self.handleNextTweet()
            else:
                print 'No tweets, sleep for 1 second'
                time.sleep(1)

    def handleNextTweet(self):
        global queueStream
        status = queueStream.pop(0)
        print self.status_wrapper.fill(status.text)
        print '\n %s  %s  via %s\n' % (status.author.screen_name, status.created_at, status.source)


def addToQueue(status):
    print 'adding tweets to the queue'
    queueStream.append(status)

    #debug
    if queueStream:
        print 'queueStream is non-empty'

if __name__ == '__main__':
    try:
        runner = RunStream()
        runner.start()
        flow = FlowController()
        flow.start()
    except KeyboardInterrupt:
        print '\nGoodbye!'

EDIT::::::::::::

Thanks for the help so far. The Queue documentation is nice and has helped me write cleaner code since the get() function blocks (cool!). Anyway, it still did not solve my problem, but I printed out the queueStream instance before passing it to FlowController and after, and they had two different memory locations. That is why I believe nothing is being popped from the queue in FlowController. Does that mean that Python passes queueStream by value and not by reference? If so, how do I get around that?

from stream import *
from textwrap import TextWrapper
from threading import Thread
from Queue import Queue
import time


class FlowController(Thread):
    def __init__(self, queueStream):
        Thread.__init__(self)
        self.queueStream=queueStream

    def run(self):
        while True:
            status = self.queueStream.get()
            print self.status_wrapper.fill(status.text)
            print '\n %s  %s  via %s\n' % (status.author.screen_name, status.created_at, status.source)


def addToQueue(status):
    print 'adding tweets to the queue'
    queueStream.put(status)

queueStream = Queue()
if __name__ == '__main__':
    try:
        runner = RunStream()
        runner.start()
        flow = FlowController(queueStream)
        flow.start()
    except KeyboardInterrupt:
        print '\nGoodbye!'
  • 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-16T21:47:48+00:00Added an answer on May 16, 2026 at 9:47 pm

    It’s hard to debug this problem without seeing RunStream.
    So I tried to dream up a simple RunStream that might exhibit the problem.

    I wasn’t able to reproduce the problem, but this code seems to work.
    If it does work and is similar enough to your RunStream, perhaps you can compare this code to your own to find what’s wrong.

    import threading
    import time
    import Queue
    import sys
    import random
    
    class FlowController(threading.Thread):
        def __init__(self,queueStream):
            threading.Thread.__init__(self)        
            self.queueStream=queueStream
        def run(self):
            while True:
                if not self.queueStream.empty():
                    print 'Handling tweet'
                    self.handleNextTweet()
                else:
                    print 'No tweets, sleep for 1 second'
                    time.sleep(1)
        def handleNextTweet(self):
            status = self.queueStream.get()
            print(status)
    
    class RunStream(threading.Thread):
        def __init__(self,queueStream):
            threading.Thread.__init__(self)
            self.queueStream=queueStream
        def run(self):
            i=0
            while True:
                addToQueue(self.queueStream,i)
                i+=1
                time.sleep(random.randint(0,2))
    
    def addToQueue(queueStream,status):
        print 'adding tweets to the queue'
        queueStream.put(status)
        if not queueStream.empty():
            print 'queueStream is non-empty'
    
    if __name__ == '__main__':
        queueStream = Queue.Queue()
        try:
            runner = RunStream(queueStream)
            runner.daemon=True
            runner.start()
            flow = FlowController(queueStream)
            flow.daemon=True
            flow.start()
            time.sleep(100)
        except KeyboardInterrupt:
            pass
        finally:
            print('Bye!')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server that receives various xml messages from clients (one thread per
I have one thread that is receiving data over a socket like this: while
Let's say that I have the following code that's run in one thread of
I have an application that has two threads. The first one (the main thread)
Say I have the following code that inserts users into MS Dynamics in a
I have a Perl script that launches 2 threads,one for each processor. I need
Is openpyxl thread safe? I'm hoping to have one thread changing the worksheet while
I have a list where one thread just does push_back and other thread occasionally
Assume I have one threadpool and each thread is running following method: void runMe(HashMap
I have two threads, one thread processes a queue and the other thread adds

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.