r/programminghelp • • Dec 04 '20

Python How to Pass by Reference (equivalently) in Python using threading?

I know python doesn't support pass by reference, but I'm stumped on how to get around this. I'm using a thread to run a function that accepts a queue as a parameter, and I want the queue to be updated in the main function as the thread runs. Any ideas? Here's the general idea:

class foo():  
    def readInput(self, queue):
        if(input):
        queue.put(self)

def bar:
    queue = Queue()
    thread = threading.Thread(target=foo.readInput, args=(queue, x))
    thread.start()
    while True:
        if(queue.qsize() == 2):
            print(2)
            return
4 Upvotes

3 comments sorted by

1

u/[deleted] Dec 06 '20

From a previous post you made, are you making a web scraper in python? In that case, I would use Scrapy. It handles a lot of queuing for you out of the box. If not, disregard

1

u/[deleted] Dec 07 '20

Oh nah this is for an entirely different project on my raspberry pi lol but thank you for the suggestion’