Novice!! Beg Pardon!! Look at the multi-pointing
Because of the work, the trial period needs to learn python, because the operations are all done by Python, fortunately a bit of Python foundation, know what is a list, tuples, characters, dictionaries or something, or die of the upturned.
The main steps for creating multithreading are:
1, create a function, this function is multi-threaded need to run a lot of content
2. Use the For loop to generate the corresponding function of multiple finished products (that is, the function has been given the required parameters, directly run on it)
3. Start executing functions with a For loop
4. Use a For loop to suspend the main program, wait for the child process to complete before performing other actions
--------------------The following is an example of transferring files to a different IP address at the same time, of course, this is not tested in a production environment, but tested in a test machine.
#!/usr/bin/pythonimport time,threading,sys,oscontrol_thread=threading. Semaphore (Ten) #控制一次能够运行多少的线程if len (sys.argv)! = 2:print ' error ' sys.exit () def Ready (IP): #这里, which is the program to run, equivalent to the 1th ip= of the description Ip.strip () if Control_thread.acquire (): Try:assert 0! = Os.system ('/usr/bin/rsync-az/opt/test%s:/var/tmp '% (IP)) Except:print Ip.strip (), Threading.currentthread (). GetName (), ' Send Fail ' else:print Ip.strip (), Threading.currentthread (). GetName (), ' SUCCESS ' Finally:control_thread.release () def ACTION (): file=sys.argv[1]x=[] For I in Open (FILE): #多次调用该函数, let X become a list to be manipulated, equivalent to the 2nd t=threading of the description. Thread (target=ready,args= (i,)) X.append (T) for I in range (len (x)): #开始执行, equivalent to the description of the 3rd X[i].start () for I in range (len (x)): # Wait for the end of the child process to perform the following action, equivalent to the description of the 4th x[i].join print ' Start time: ', Time.ctime (), ' \ n ' action () print ' \ n ', ' End time: ', Time.ctime (), ' \ n '
This article is from the "to be a knowledgeable ops dog" blog, please be sure to keep this source http://kekeung.blog.51cto.com/2339469/1575894
Python multi-threaded transfer file