Python multithreaded processing experiment

Source: Internet
Author: User

I'm going to do something recently, not before the optimization. It takes 11 hours to run once. When running to see the CPU, memory is rich, consider using Python multithreading to do.

Multi-threaded If you can save time, but also to wait for the IO, let the machine do something else. Otherwise, if you just calculate the loop 1+1=2, using multithreading will not improve efficiency because the CPU is busy. Each thread, will take up a piece of memory, the thread also can not get more, up more, not the CPU time, and not so much memory.

The multithreading experiment is as follows:

1. Use the loop to do it. Main thread Loop 50 times, once per cycle, perform a processing data calculation, rest 5 seconds (Analog read file)

2. Start the thread to do it. Make a list of threads, one for each cycle, and a new thread to the list. When a list is 5 or 3, wait. Check if there is a completed thread, and if so, clear out the list.

Finally compare the time difference between 1 and 2

Code for Experiment 1

Import Threadingimport time# Define a function for the Threaddef print_time (name,delay):    #print "%s worker started\n"% Name for    i in range (1,10000): for        J in Range (1,1000):            data = i*j            #print "%swork finshed\n"%name    Return    Count =0starttime = Time.time () while Count <30:    count +=1    print ' current record: ', Count    Print_time ("Hello", 1)    Time.sleep (5)    endTime = Time.time () Timediff = Endtime-starttimeprint Cost: ", Timediff

Experiment 1 Execution time approx. 178 sec.

Lab 2: How to start a thread

1 ImportThreading2 Import Time3 4 5 #Define a function for the thread6 defPrint_time (name,delay):7     #print "%s worker started\n"%name8      forIinchRange (1,10000):9          forJinchRange (1,1000):Tendata = i*J One       A     #print "%swork finshed\n"%name -     return -      the      -  - classMyThread (Threading. Thread): -     def __init__(self,threadid,name,counter): +Threading. Thread.__init__(self) -Self.threadid =ThreadID +Self.name =name ASelf.counter =counter at     defRun (self): -         Print "starting%s\n"%Self.name - print_time (self.name,self.counter) -         Print "Exiting%s\n"%Self.name -  - """ in Threadlock = Threading. Lock () - threads = [] to thread1 = myThread (1, "Thread-1", 2) + thread2 = MyThread (2, "Thread-2", 2) - Thread1.start () the Thread2.start () * threads.append (THREAD1) $ threads.append (thread2)Panax Notoginseng For T in Threads: - T.join () the print "Exiting Main Thread" + """ A  the defReadFile (): +     Print "read file ...." -Time.sleep (5) $      $Threadlock =Threading. Lock () -Threads = [] -Count =0 theStartTime =time.time () -  whileCount <30:Wuyi     Print "Current record:", Count theName="Thread"+Str (count) -Thread = MyThread (count,name,2) Wu     ifLen (Threads) <5: - Thread.Start () About threads.append (thread) $Count +=1 - ReadFile () -          -         #Thread.Join () A     ifLen (Threads) >=5: +          forTinchThreads: the             if  nott.isalive (): -                 PrintT.name,"need Remove" $ threads.remove (t) the      the  theEndTime =time.time () the  -Timediff = endtime-StartTime in Print "All work are done cost :", Timediff

Experiment 2 after running, about 151 seconds, basically calculate the use of the main thread rest time.

If the main thread does not sleep, just count+=1, then use multithreading instead run slower.

Python multithreaded processing experiment

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.