I. Introduction to the concept
Thread is one of the most important classes in the threading module and can be used to create threads. There are two ways to create a thread: one is by inheriting from the Thread class and overriding its run method; the other is to create an object and pass the callable object as an argument in its initialization function (__init__).
The Thread module is a low-level module, and the Threading module is a wrapper around Thread that can be used more easily.
In addition, it is sometimes necessary to execute multiple commands concurrently, rather than sequentially.
II. Code Samples
#!/usr/bin/python # encoding=utf-8 # Filename: # Inherit directly from Thread, create a new class, and put the code that the thread executes into this new class import threading import time class ThreadImpl(): def __init__(self, num): .__init__(self) self._num = num def run(self): global total, mutex # Print the thread name print ().getName() for x in xrange(0, int(self._num)): # Acquire the lock () total = total + 1 # Release the lock () if __name__ == '__main__': # Define global variables global total, mutex total = 0 # Create locks mutex = () #Define the thread pool threads = [] # Create thread objects for x in xrange(0, 40): (ThreadImpl(100)) # Starting threads for t in threads: () # Wait for the child thread to finish for t in threads: () # Print the results of the execution print total
#!/usr/bin/python # encoding=utf-8 # Filename: # Create a function for the thread to execute, pass this function into the Thread object and let it execute it import threading import time def threadFunc(num): global total, mutex # Print the thread name print ().getName() for x in xrange(0, int(num)): # Acquire the lock () total = total + 1 # Release the lock () def main(num): # Define global variables global total, mutex total = 0 # Create locks mutex = () #Define the thread pool threads = [] # Create the thread object first for x in xrange(0, num): ((target=threadFunc, args=(100,))) # Start all the threads for t in threads: () # Wait for all child threads to exit in the main thread for t in threads: () # Print the results of the execution print total if __name__ == '__main__': # 40 threads created main(40)
#!/usr/bin/python # encoding=utf-8 # Filename: put_files_hdfs.py # Let multiple commands executed concurrently, such as letting multiple scp, ftp, hdfs upload commands executed concurrently, to improve program efficiency import datetime import os import threading def execCmd(cmd): try: print "Command %s to start running %s" % (cmd,()) (cmd) print "Command %s end run %s" % (cmd,()) except Exception, e: print '%s\t Failed to run, reason for failure\r\n%s' % (cmd,e) if __name__ == '__main__': # List of commands to be executed cmds = ['ls /root', 'pwd',] #Thread Pool threads = [] print "Program started running %s" % () for cmd in cmds: th = (target=execCmd, args=(cmd,)) () (th) # Wait for the thread to finish running for th in threads: () print "End of program run %s" % ()
This is the whole content of this article, I hope to help you learn python programming.