Because there are too many demos for Python Thread Programming, you cannot upload them here. Therefore, if you have a communitysever, you can obtain it from it and decompile it for your own use. If you do not have one, search for it on the network, there are many resources for everyone to learn and think about.
To use a thread in Python Thread Programming, python lib provides two methods. One is a function type, and the other is a thread object encapsulated by a class. Here are two simple examples to help you. For more information about multithreaded programming, such as mutex, semaphore, and critical section, see the Python thread programming documentation and related materials.
1. Call the start_new_thread () function in the thread module to generate a new thread. Please refer to the Code:
- # Thread_example.py
- Import time
- Import thread
- Def timer (no, interval): # self-written thread function
- While True:
- Print 'thread :( % d) Time: % s' % (no, time. ctime () time. sleep (interval)
-
-
- Def test (): # Use thread. start_new_thread () to generate two new threads.
- Thread. start_new_thread (timer, (1, 1 ))
- Thread. start_new_thread (timer, (2, 3 ))
-
-
- If_ Name __= '_ Main __':
- Test ()
This is thread. start_new_thread (function, args [, kwargs]) function prototype. The function parameter is the thread function to be called. The args parameter is the parameter that is passed to your thread function, it must be a tuple type, and kwargs is an optional parameter. The end of a thread generally depends on the natural end of a thread function. You can also call a thread in a thread function. exit (), which throws SystemExit exception to exit the thread.
2. Call the threading module to inherit the threading. Thread class to encapsulate a Thread object. See the Code:
- Import threading
- Import time
- Class timer (threading. Thread): # My timer class inherits from the threading. Thread class.
- Def _ init _ (self, no, interval ):
- # When I override the _ init _ method, remember to call the _ init _ method of the base class.
- Threading. Thread. _ init _ (self)
- Self. no= No
- Self. interval= Interval
- Def run (self): # rewrite the run () method and put the code of your thread function here
- While True:
- Print 'thread Object (% d), Time: % s' % (self. no, time. ctime ())
- Time. sleep (self. interval)
- Def test ():
- Threadone=Timer() # Generate two thread objects
- Threadtwo=Timer(2, 3)
- Threadone. start () # activate a thread by calling the. start () method of the thread object
- Threadtwo. start ()
- If_ Name __= '_ Main __':
- Test ()
In fact, the thread and threading modules also contain many other things about multithreading programming, such as locks, timers, and list of activated threads. Please refer to the Python thread programming documentation carefully!
- How to embed Python into C ++ applications?
- In-depth discussion of Ruby and Python syntax comparison
- Introduction to Python
- Python Learning Experience: version, IDE selection and coding Solutions
- Analysis of Python GIL and thread security