Python multithreaded threading

Source: Internet
Author: User

Referring to the thread module, we have to solve the thread lock ourselves. Actually, there's nothing. Just now people are more lazy, since there is a high-level package of functions why to write their own.

So there is the threading.

It's all the same, actually.

One of the simplest threading codes:

1 ImportThreading#Import thrteading Module2 3 defRun ():#thread function, print thread name and I value4      forIinchRange (100):5         Print(Threading.current_thread (). GetName () +"-------"+str (i))6 7Threads = []#Create a list of threads8 9  forIinchRange (10):#Create a thread and add a list of threads into the threadTent = Threading. Thread (target =run) One      A threads.append (t) -  -  forIinchThreads#Start All Threads the I.start () -      -  forIinchThreads#Main Line waits for all child threads to end -I.join ()

The above code differs from the thread module only in that the join () function replaces us to write the thread lock.

That's all.

If it can be simpler:

1 Import Threading 2 3 def run (): 4      for  in range:5         print(i)67 t = Threading. Thread (target = run)8t.start ()9 t.join ()

The main function does nothing but start a sub-thread.

Python multithreaded threading

Related Article

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.