On Python multithreading and queue Management Shell program

Source: Internet
Author: User
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 gevent def Servers (): Servers=commands.getoutput ("' ls/data/program/ payment/| grep ' payment ') servers=servers.split (' \ n ') return servers def 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 ' r=commands.getoutput (' Su-tomcat- C "/data/program/payment/%s/bin/server.sh%s &" (Servername,sys.argv[1]) #在这里会阻塞, we cannot find a suitable place to perform the switch gevent of the co-process.    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:thread S.append (Gevent.spawn (handle,i)) # 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 Threadi ng Import Thread _sentinel = Object () def Servers (): Servers=commands.getoutput (' ls/data/program/payment/| grep ' Paym Ent ") servers=servers.split (' \ n ') return servers Def producer (SERVERNAME,OUT_Q): if sys.argv[1] = = ' Start ' or SYS.A RGV[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 the end of the Loop 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)) #消费者                   Gets the result in the queue, the previous function inside has been looped for the for I in S:t2=thread (Target=producer, args= (I,q,)) #讲线程进行管理, put into the queue T2.start () #启动生产者线程 # T2.join () #启动生产者以后放弃校验线程是否结束, concurrency, because we are putting threads in the queue to manage, so we don't have to wait here for the thread to end, if we use a join here it will block our process Order.  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.

The above mentioned is the whole content of this article, I hope you can like.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.