Talk:Synchronous concurrency: Difference between revisions

New section: Python
(much clearer)
(New section: Python)
Line 9:
::::I have no problem with your clarification. --[[User:Waldorf|Waldorf]] 20:55, 21 October 2007 (MDT)
::::Now it is much clearer. Should there be a similar task that allows communication between processes? The [[Fork Process]] is the closest I can find, but doesn't involve communication. [[User:Ahy1|Ahy1]] 23:11, 21 October 2007 (MDT)
 
== Python ==
 
Alernatively the ''write()'' function here could be replaced with a '''''Writer''''' class like:
 
class Writer(object):
def __init__(self, filehandle, queue):
self.linecount = 0
if not hasattr(queue, 'get'):
raise TypeError, 'filehandle must support "write()" method'
else:
self.file = filehandle
if not hasattr(queue, 'get'):
raise TypeError, 'Queue handle must support "get()" method'
self.queue = queue
def __call__(self):
while True:
line = self.queue.get()
if line is None:
break
self.file.write(line)
self.linecount += 1
 
:There is not point in checking for filehandle attributes, if someone send you broken filehandle, it will raise AttribtueError anyway.
 
... which keep the "linecount" attribute encapsulated to allow the main code path
 
:The line count is already encapsulated in the write function. Why use a class when you can get the job done with a simple function?
 
to access it separately using something like:
 
write = Writer(sys.stdout, lines)
reader = Thread(target=read, args=(open('input.txt'),))
writer = Thread(target=write)
reader.start()
writer.start()
reader.join()
writer.join()
print "Line count: ", write.linecount
 
:Accessing the line count like this require synchronization between the reader and the writer and locking. Using a queue as in the example code make everything simpler and safer. as you explain bellow :-)
 
... (Though this also requires that we remove the final ''print'' statement from the ''read()'' function --- otherwise the reader() thread won't "join" because of the last item remaining it the "count" queue).
 
:The task description ask the reader to print.
 
In general it's cleaner to use the Queue objections for inter-thread communications in lieu of explicit, error prone and complicated locking. Python Queue objects are coherent thread-safe "producer-consumer" pipelines which are suitable for any combination of single or multiple producer and consumer threads. Since objects of any sort can be passed through a Queue it would be trivial to encapsulate each line read in an object specifying a target file object along with the data to be written. The ''Writer'' instance could then count each of these as it called something like "line.write(line.data)" (for example). Obviously the ''read()'' function in the original example could also be replaced with a class which could allow it to maintain any desired state or implement additional behavior.
 
:Obviously you can write this in many complecated ways, but here we need the simplest way that conform with the task description and is a good example of using Python for this task.
Anonymous user