954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

python threads integrated with queues

I want to have two threads with a queue exchanging data (in the form of lists) between the two threads. However, at the moment I cannot fathom out how to write the code simply for taking the first list from the queue, processing it(which I have written the function for) and putting the returned data into a different queue. Can anyone give me any hints please?

I don't really understand the thread and queue modules in python, so am a bit stuck.

machine91
Newbie Poster
5 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

It's pretty easy:

from threading import Thread
from Queue import Queue

# first create the 2 queues you need
quxxx = Queue(10)
quyyy = Queue(10)

# then write the functions that the 2 threads will execute.
# in the functions, put and get items from the queues as you like.
# don't forget to call queue.task_done() after each queue.get()
def funcA():
    pass

def funcB():
    pass

# then create the 2 threads
thA = Thread(target=funcA)
thB = Thread(target=funcB)

# then start the threads
thA.start()
thB.start()

# then wait for the threads completion
thA.join()
thB.join()

# the end
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

It's pretty easy:

from threading import Thread
from Queue import Queue

# first create the 2 queues you need
quxxx = Queue(10)
quyyy = Queue(10)

# then write the functions that the 2 threads will execute.
# in the functions, put and get items from the queues as you like.
# don't forget to call queue.task_done() after each queue.get()
def funcA():
    pass

def funcB():
    pass

# then create the 2 threads
thA = Thread(target=funcA)
thB = Thread(target=funcB)

# then start the threads
thA.start()
thB.start()

# then wait for the threads completion
thA.join()
thB.join()

# the end


Many thanks for this it is a real help.
However, if I simply wanted to start only one thread that was seperate from the main thread, and use the two queues to pass data between the main thread and the other thread could I just use the start_new_thread function?

machine91
Newbie Poster
5 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 
Many thanks for this it is a real help. However, if I simply wanted to start only one thread that was seperate from the main thread, and use the two queues to pass data between the main thread and the other thread could I just use the start_new_thread function?


If you want to start a single thread, write the same code without funcB() and thB. The main thread can interact with thA through queues between thA.start() and thA.join().
It's not a good idea to use directly the methods of the thread module, like start_new_thread(). As the documentation says, this module contains low level functions. Use the module threading instead. Errors in thread programming may be difficult to track, and the module threading is safer.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: