Parallel(?) programming with python
MRAB
python at mrabarnett.plus.com
Mon Aug 8 15:18:16 EDT 2022
On 2022-08-08 12:20, Stefan Ram wrote:
> Andreas Croci <andrea.croci at gmx.de> writes:
>>Basically the question boils down to wether it is possible to have parts
>>of a program (could be functions) that keep doing their job while other
>>parts do something else on the same data, and what is the best way to do
>>this.
>
> Yes, but this is difficult. If you ask this question here,
> you might not be ready for this.
>
> I haven't learned it yet myself, but nevertheless tried to
> write a small example program quickly, which might still
> contain errors because of my lack of education.
>
> import threading
> import time
>
> def write_to_list( list, lock, event ):
> for i in range( 10 ):
> lock.acquire()
> try:
> list.append( i )
> finally:
> lock.release()
> event.set()
> time.sleep( 3 )
>
> def read_from_list( list, lock, event ):
> while True:
> event.wait()
> print( "Waking up." )
> event.clear()
> if len( list ):
> print( "List contains " + str( list[ 0 ]) + "." )
> lock.acquire()
> try:
> del list[ 0 ]
> finally:
> lock.release()
> else:
> print( "List is empty." )
>
> list = []
> lock = threading.Lock()
> event = threading.Event()
> threading.Thread( target=write_to_list, args=[ list, lock, event ]).start()
> threading.Thread( target=read_from_list, args=[ list, lock, event ]).start()
>
> In basketball, first you must learn to dribble and pass,
> before you can begin to shoot.
>
> With certain reservations, texts that can be considered
> to learn Python are:
>
> "Object-Oriented Programming in Python Documentation" - a PDF file,
> Introduction to Programming Using Python - Y Daniel Liang (2013),
> How to Think Like a Computer Scientist - Peter Wentworth (2012-08-12),
> The Coder's Apprentice - Pieter Spronck (2016-09-21), and
> Python Programming - John Zelle (2009).
>
When working with threads, you should use queues, not lists, because
queues do their own locking and can wait for items to arrive, with a
timeout, if desired:
import queue
import threading
import time
def write_to_item_queue(item_queue):
for i in range(10):
print("Put", i, "in queue.", flush=True)
item_queue.put(i)
time.sleep(3)
# Using None to indicate that there's no more to come.
item_queue.put(None)
def read_from_item_queue(item_queue):
while True:
try:
item = item_queue.get()
except item_queue.Empty:
print("Queue is empty; should've have got here!", flush=True)
else:
print("Queue contains " + str(item) + ".", flush=True)
if item is None:
# Using None to indicate that there's no more to come.
break
item_queue = queue.Queue()
write_thread = threading.Thread(target=write_to_item_queue,
args=[item_queue])
write_thread.start()
read_thread = threading.Thread(target=read_from_item_queue,
args=[item_queue])
read_thread.start()
# Wait for the threads to finish.
write_thread.join()
read_thread.join()
print("Finished.")
More information about the Python-list
mailing list