I. Practice REQUIREMENTS:
Write a simple text processing tool with three tasks, one to receive user input, one to format user input, and one to file formatted results
two. Analysis:
Three tasks, that is, three threads. The input, conversion, and writing of these 3 threads. So what are the inputs that are written, and how are the other threads taken out? We can use the queue queues to write the characters entered by the user into the queue before converting. After the conversion is complete, And then write to another queue. Wait for the last write file
Three. Implement
1 fromThreadingImportThread2 fromMultiprocessingImportQueue3 4 5 defINP (q):6 whileTrue:7INP_STR = input (">>>>"). Strip ()8 q.put (INP_STR)9 ifInp_str = ="Exit":Ten Break One A - defstr_up (Q, Q1): - whileTrue: themsg =q.get (). Upper () - q1.put (msg) - ifmsg = ="EXIT": - Break + - + defDump_f (Q1): A whileTrue: at ifQ1.get ()! ="EXIT": -With open ("Test.txt","A +", encoding="Utf-8") as F: -F.write (Q1.get () +"\ n") - Else: - Break - in - if __name__=='__main__': toQ =Queue () +Q1 =Queue () -T1 = Thread (TARGET=INP, args=(q,)) theT2 = Thread (target=str_up, args=(q, Q1)) *T3 = Thread (Target=dump_f, args=(Q1,)) $ T1.start ()Panax Notoginseng T2.start () -T3.start ()
Simple Text Processing
Exercises, using multithreading to write a simple text processing tool