Exercises, using multithreading to write a simple text processing tool

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.