First of all to describe the environment, there are many Java programs on the machine, we have a startup | stop | Restart script in each Java program
As an example:
We are now going to run these scripts at the same time to get started all the Java programs quickly, if we only use multithreading, the thread will not return the message to the parent process, how can we know that these programs are started successfully?
So we used the queue to manage it.
"" "I tried gevent, but I would cause a blockage in command here."
Gevent code as follows if a friend knows how to optimize, please tell me
#!/usr/bin/python2.7# -*- coding:utf-8 -*-import os,sysfrom datetime import datetimeimport commandsimport gevent.monkeygevent.monkey.patch_os () import geventdef Servers (): servers=commands.getoutput ("' ls /data/program/payment/ | grep ' payment ' ') servers=servers.split (' \ n ') return serversdef handle (ServerName): if sys.argv[1] == ' start ' or sys.argv[1] == ' Stop ' or sys.argv[1] == ' restart ': print ' \033[1;31;40m ' print ' ========================>>>go to handle %s<<<==================== ===== ' %servername print ' \033[0m ' &nbsP;r=commands.getoutput ("su - tomcat -c "/data/program/payment/%s/bin/server.sh %s & " " % (servername,sys.argv[1]) #在这里会阻塞, we can't find the right place to switch the co-process gevent.sleep (0) #无论放到何处, either before or after the switch will block. print r else: print ' Please use start | stop | restart to handle the command ' sys.exit (1) if __name__ == ' __main__ ': s=servers () threads=[] for i in s: threads.append (Gevent.spawn (handle,i)) # &nbsP;print threads gevent.joinall (Threads)
Multithreaded code is as follows
#!/usr/bin/python2.7# -*- coding:utf-8 -*-from datetime import datetimeimport commandsfrom Queue import Queuefrom threading import Thread_sentinel = object () def servers (): servers=commands.getoutput ("ls /data/program/ payment/ | grep ' payment ' ') servers=servers.split (' \ n ') return serversdef producer (servername,out_q): if sys.argv[1] == ' start ' or sys.argv[1] == ' Stop ' or sys.argv[1] == ' Restart ': print ' \033[1;31;40m ' print ' ========================>>>put %s in queue <<<========================= ' %servername print ' \033[0m ' out_q.put_nowait (Commands.getoutput ("su - tomcat -c "/data/program/payment/%s/bin/server.sh %s &" " % (Servername,sys.argv[1])) #放入队列的对象 else: print ' please use start | stop | Restart to handle the command ' sys.exit (1) Def consumer (servername,in_q): n=len (ServerName) while n > 0: #循环在队列中取结果 until end of cycle data=in_q.get () n -= 1 print ' \033[1;31;40m ' print data print ' \033[0m ' print ' \ 033[1;31;40m ' print ' Consumer was done!!!!!!! ' print ' \033[0m ' if __name__ == ' __main__ ': s=servers () q = queue () t1 = thread ( Target=consumer, args= (S,q,)) # The consumer gets the result in the queue, the previous function has been looped inside for i in s: t2=thread (target=producer, args= (I,q,)) #讲线程进行管理, put the queue t2.start () #启动生产者线程 # t2.join () # Starting the producer discards the check thread for the end and concurrency, because we put the thread into the queue to manage it, so we don't have to wait here for the thread to end, and if we use a join it will block our program. After the thread finishes, the consumer notifies the parent that the thread of the process has ended. t1.start () #启动消费者线程 T1.join () #在获取完成之前进行线程的阻塞
Simply say join this method:
Calling Thread.Join will cause the thread to block until the calling thread finishes running or times out. The parameter timeout is a numeric type that indicates the time-out period, and if the parameter is not supplied, the thread will block until the thread ends.
Using Python multithreading and queuing to manage shell programs