Python multi-thread application

Source: Internet
Author: User

Python supports multiple threads. Allows you to quickly create threads, mutex locks, semaphores, and other elements, and supports synchronization and mutex between read and write threads. In the US, Python runs on a python virtual machine. The multi-thread created may be a virtual thread and needs to be polling and scheduled by the python virtual machine, which greatly reduces the availability of Python multi-thread. Today, we have used classic producer and consumer issues to explain how to use multiple threads in Python.Code:

# Encoding = UTF-8
Import Threading
Import Random
Import Time
From Queue Import Queue

Class Producer(Threading.Thread):

Def _ Init __ ( Self , Threadname , Queue ):
Threading . Thread . _ Init __ ( Self , Name = Threadname )
Self . Sharedata = Queue

Def Run ( Self ):
For I In Range ( 20 ):
Print Self . Getname (), 'Adding' , I , 'To queue'
Self . Sharedata . Put ( I )
Time . Sleep ( Random . Randrange ( 10 ) / 10.0 )
Print Self . Getname (), 'Finished'

# Consumer thread

Class Consumer(Threading.Thread):

Def _ Init __ ( Self , Threadname , Queue ):
Threading . Thread . _ Init __ ( Self , Name = Threadname )
Self . Sharedata = Queue

Def Run(Self):

For I In Range ( 20 ):
Print Self . Getname (), 'Got a value :' , Self . Sharedata . Get ()
Time . Sleep ( Random . Randrange (10 ) / 10.0 )
Print Self . Getname (), 'Finished'

# Main thread

Def Main():

Queue = Queue ()
Producer = Producer ( 'Producer' , Queue )
Consumer = Consumer ( 'Sumer' , Queue )
Print 'Starting threads ...'
Producer . Start ()
Consumer . Start ()
Producer . Join ()
Consumer . Join ()
Print 'All threads have terminated .'
If _ Name __ = '_ Main __' :
Main ()

you may feel different when running this code disconnection yourself! After understanding this, you can use Python cookielib and then use Python urllib to write a multi-thread download web page script. The author is: Old Wang @ Python tutorial Old Wang python, python tutorials and Python downloads related to pythn are provided. I hope you will like them

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.