Python uses the object-oriented method to create a thread to implement the 12306 ticket sales system

Source: Internet
Author: User
Currently, python provides several multi-threaded implementation methods, such as thread, threading, and multithreading. The thread module is relatively low-level, while the threading module packages thread, it can be used more conveniently. Currently, python provides several multi-threaded implementation methods, such as thread, threading, and multithreading. The thread module is relatively low-level, and the threading module is used to pack the thread, it can be used more conveniently.

Introduction to object-oriented technology

Class: a set of objects with the same attributes and methods. It defines the attributes and Methods shared by each object in the set. The object is a class instance.

Class variables:Class variables are public in the entire instantiated object. Class variables are defined in the class and outside the function body. Class variables are generally not used as instance variables.

Data member:Class variables or instance variables are used to process data related to the class and its instance objects.

Method Rewriting:If the method inherited from the parent class cannot meet the requirements of the subclass, you can rewrite it. This process is called override, or method rewriting.

Instance variables:The variables defined in the method only act on the class of the current instance.

Inheritance:That is, a derived class inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where a Dog-type object is derived from the Animal class, which simulates the relationship "is a (is-a)" (example diagram, Dog is an Animal ).

Instantiation:Create an instance of a class and a specific object of the class.

Method:Class.

Object:Data Structure instance defined by class. The object includes two data members (class variables and instance variables) and methods.

--- Restore content start ---

Multi-threading is implemented through the object-oriented method. Its core is to inherit the threading. Thread class. We have defined a class BoothThread, which inherits from the thread. Threading class and defines the command to be executed by the Thread by modifying the run () method of the thread class.

Import threading # Python mainly uses the threading package in the standard library to implement multi-threaded import timeimport OS # As the interval for each call. sdef doChore (): time. sleep (.) # define a class BoothThread to inherit from thread. threading class BoothThread (threading. thread): def _ init _ (self, tid, monitor): self. tid = tidself. monitor = monitorthreading. thread. _ init _ (self) def run (self): while True: monitor ['lock']. acquire () # Call lock. acquire () locks if monitor ['tick']! =: Monitor ['tick'] = monitor ['tick']-# reduce a print (self. tid, ': now left:', monitor ['tick']) # number of remaining votes doChore () else: print ("Thread_id", self. tid, "No more tickets") OS. _ exit () # exit program monitor ['lock']. release () # release lock doChore () monitor = {'tick':, 'lock': threading. lock ()} # Number of initialization votes # A total of threads are set for k in range (): new_thread = BoothThread (k, monitor) # create thread; Python uses threading. thread object to represent the thread class BoothThread inherited from Thread. threading class new_thread.start () # Call the start () method to start the thread

A dictionary monitor is used to store global variables and pass the dictionary as a parameter to the thread function. Since a dictionary is a variable data object, when it is passed to a function, the function still uses the same object, which is equivalent to being shared by multiple threads.

The above content introduces all the descriptions about how Python uses the object-oriented method to create a thread to implement the 12306 ticket sales system.

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.