Multithreading in python (threading, queue, event, paramiko)

First of all, let's talk about multithreading in pyhton, because it directly calls the underlying C library and does not have its own underlying method, so it is not as good as other languages. It should be regarded as a defect in python.

In multithreading, to introduce the module threading, when using, create a thread instance object through T1=threading.Thread(target=func, args=('arg1',''arg2). Use T1.start() to Start the thread.

Take a look at the specific code:

1  #You can see the general process, but the thread lock in it does not seem to work. Mainly look at the usage of daemon threads and the usage of JOIN. 
2  
3  import threading,time
 4 lock= threading.Lock()
 5 num=1
 6  def run():
 7      lock.acquire() #Thread lock , invalid for versions above 3.0. 
8      global num
 9      num=num*2
 10      lock.release()
 11 t_obj= []
 12 start_time= time.time()
 13  for i in range(50 ):
 14      t=threading.Thread(target= run)
 15     # t.setDaemon(True) #Set this thread as the daemon thread of the main thread. 
16      t.start()
 17      t_obj.append(t)
 18  for i in t_obj:
 19      i.join()
 20  print ( ' num: ' ,num)
 21  # print ('done time is ',time.time() -start_time)

In thread knowledge, there is an EVEVT that is more practical.

#You can see that the Event here is similar to a global variable with two values: True False 
import
threading, time event=threading.Event() def light(): count=0 event.set() while True: if count>3 and count <=5: event.clear() print('Red light') elif count>5: event.set() print('Green light') count=0 else: print ('runing....') count+=1 def car(name): while True: if event.is_set(): print('car %s is run.....'%name) else: print('car %s is wait.....'%name) event.wait() l1=threading.Thread(target=light,) l1.start() c1=threading.Thread(target=car,args='Weiz') c1.start() # event.wait() (waiting for the flag to be set) event.set() (setting the flag) event.clear() I (clearing the flag setting) event.is_set() (judging whether to set the flag) Four methods.

In addition, the combination of threads and queues is conducive to the realization of "high cohesion and low coupling" and improves efficiency.

''' PYTHON demo implementation of the producer-consumer pattern. Main knowledge points: The queue takes the production and eating of dumplings as an example '''

import threading,queue,time #Thread queue date 
q=queue.Queue(maxsize=10)   # first in first out 
# q1=queue.LifoQueue(maxsize=10) last in first out 
# q2=queue.PriorityQueue(maxsize=10 ) can set priority 
def product():
    n=1
    while True:
        q.put( ' Dumpling %s ' % n)
         print ( ' Dumpling produced ' ,n)
        time.sleep(0.2)
        n+=1

def consumer(name):
    while True:
        print("%s eating %s"%(name,q.get()))
        time.sleep(0.1)
p=threading.Thread(target=product,)
p.start()
c=threading.Thread(target=consumer,args=('Jack',))
c.start()
c1=threading.Thread(target=consumer,args=('Tom',))
c1.start()

In addition to the above knowledge, there is also a paramiko module, which can implement SSH client. Using multi-threading, it can realize batch management of multiple hosts, execute commands in batches, and send files in batches.

Code example: execute instruction.

1  import paramiko
 2  #Create SSH object 
3 ssh = paramiko.SSHClient()
 4  #Allow connections to hosts not in the know_hosts file 
5  
6  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 7  #Connect to server 
8 ssh.connect(hostname= ' 10.0.0.31 ' , port=52113, username= ' root ' , password= ' 123456 ' )
 9  #Execute command 
10 stdin, stdout, stderr = ssh.exec_command( ' df ' )
 11 #Get the command result 
12 res,err = stdout.read(),stderr.read()
 13 result = res if res else err
 14  
15  print (result.decode())
 16  
17  #Close the connection 
18 ssh.close()

Code Example: Sending a file.

import paramiko
transport = paramiko.Transport(('10.0.0.31', 52113))
transport.connect(username='root', password='123456')
sftp = paramiko.SFTPClient.from_transport(transport)
 #Upload file1 to the server /tmp/ sftp.put(file1, '/tmp') #Download file2 to the local local_path 
sftp.get 
(file2_path, ' fromlinux.txt ' )

transport.close()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325119393&siteId=291194637