def test(): NUMBER_OF_PROCESSES = 4 TASKS1 = [(mul, (i, 7)) for i in range(20)] TASKS2 = [(plus, (i, 8)) for i in range(10)] # Create queues task_queue = Queue() done_queue = Queue() # Submit tasks task_queue.putMany(TASKS1) # Start worker processes for i in range(NUMBER_OF_PROCESSES): Process(target=worker, args=(task_queue, done_queue)).start() # Get and print results print 'Unordered results:' for i in range(len(TASKS1)): print '\t', done_queue.get() # Add more tasks using `put()` instead of `putMany()` for task in TASKS2: task_queue.put(task) # Get and print some more results for i in range(len(TASKS2)): print '\t', done_queue.get() # Tell child processes to stop for i in range(NUMBER_OF_PROCESSES): task_queue.put('STOP')
def load(): queue=Queue() hostfile_line=open(sys.argv[2],'r').readlines() source=sys.argv[3] destdir=sys.argv[4] for hostfile in hostfile_line: eachline=hostfile.split() queue.put(eachline) eachline=Process(target=TRANS,args=(queue.get(),source,destdir)) eachline.start() eachline.join()
def exe(): queue=Queue() hostfile_line=open(sys.argv[2],'r').readlines() command_file=open(sys.argv[3],'r').readlines() for command_line in command_file: command_list=command_line.split('\n') command=''.join(command_list) for hostfile in hostfile_line: eachline=hostfile.split() queue.put(eachline) eachline=Process(target=SSH,args=(queue.get(),str(command))) eachline.start() eachline.join()
ips = IP("10.0.1.0/24") def f(i, q): while True: if q.empty(): sys.exit() print "Process Number: %s" % i ip = q.get() ret = subprocess.call("ping -c 1 %s" % ip, shell=True, stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT) if ret == 0: print "%s: is alive" % ip else: print "Process Number: %s didn't find a response for %s" % (i, ip) for ip in ips: q.put(ip) #q.put("192.168.1.1") for i in range(50): p = Process(target=f, args=[i, q]) p.start() print "main process joins on queue" p.join() print "Main Program finished"
stderr=subprocess.STDOUT) # Check return value; if else than zero inform user if retVal == 0: mf.Cout("Process #%s is alive." % (iProcess)) else: mf.Cout("Process #%s is not responding for IP Address %s." % (iProcess, ip)) if __name__ == "__main__": mf.StopWatchStart() # Loop over all IP addresses for ip in ipAddresses: # Put an item into the queue queue.put(ip) # Loop over a given number of processes; for iProcess in range(nProcesses): # Create process p = Process(target=f, args=[iProcess, queue]) # Start process p.start() mf.Cout("Main process joins on queue.") # Join procees to on queue so that all processes are gotten and processed before exiting the program. p.join() mf.Cout("Main program finished.") #timer.sleep(5) mf.StopWatchStop()
#-*- coding: utf-8 -*- from processing import Process, Queue import time def f(q): x = q.get() print 'Process Number %s, sleeps for %s seconds' % (x, x) time.sleep(x) print 'Process Number %s finished' % x q = Queue() for i in range(10): q.put(i) p = Process(target=f, args=[q]) p.start() print 'main process joins on queue' p.join() ''' 为什么 main process joins on queue 出现的位置不固定, 难道是因为给进程分配资源需要时间而导致的么??? ''' print 'main process finished'
import subprocess from IPy import IP import sys q = Queue() ips = IP("10.0.1.0/24") def f(i,q): while True: if q.empty(): sys.exit() print "Process Number: %s" %i ip = q.get() ret = subprocess.call("ping -c 1 %s"%ip, shell=True,stdout=open('/dev/null','w'),stderr = subprocess.STDOUT) if ret == 0 : print "%s: is alive"%ip else: print "Process Number: %s didn't find a response for %s"%(i,ip) for ip in ips: q.put(ip) #q.put("192.168.1.1") for i in range(50): p = Process(target=f, args=[i,q]) p.start() print "main process joins on queue" p.join() print "Main Program finished"
def algo(request, algo): text = "" type = "" algo_object = get_object_or_404(Algo, shortTitle=algo) manual = get_object_or_404(ManPage, algo=algo_object) embedFormDict = { 'cpt' : CPTEmbedForm, 'f5' : F5EmbedForm, 'lsb' : LsbEmbedForm, 'gifshuffle' : GifShuffleEmbedForm, 'bs' : BattlestegEmbedForm, } extractFormDict = { 'cpt' : CPTExtractForm, 'f5' : F5ExtractForm, 'lsb' : LsbExtractForm, 'gifshuffle' : GifShuffleExtractForm, 'bs' : BattlestegExtractForm, } typeDict = { 'cpt' : "png", 'f5' : "jpeg", 'lsb' : "png", 'gifshuffle' : "gif", 'bs' : "png", } if request.method == 'POST': q = Queue() # embedding if "submit1" in request.POST: algoDict = { 'cpt' : cptEmbed, 'f5' : f5Embed, 'lsb' : lsbEmbed, 'gifshuffle' : gifShuffleEmbed, 'bs' : bsEmbed, } embedForm = embedFormDict[algo](request.POST, request.FILES) extractForm = extractFormDict[algo]() type = typeDict[algo] p = Process(target=algoDict[algo], args=(q, )) # fork process to embed if embedForm.is_valid(): p.start() q.put([request.POST, request.FILES['file'].temporary_file_path()]) os.system("sleep 1") try: retval = q.get(True, 10) except Q.Empty: retval = -2 p.join() if retval == -1: text += "%s-Datei nicht gefunden oder fehlerhaft."%(type) elif retval == -2: text += "Fehler beim Einbetten. Anderes Bild oder andere Parameter versuchen." else: return createResponse(retval, type) # extracting elif "submit2" in request.POST: algoDict = { 'cpt' : cptExtract, 'f5' : f5Extract, 'lsb' : lsbExtract, 'gifshuffle' : gifShuffleExtract, 'bs' : bsExtract, } embedForm = embedFormDict[algo]() extractForm = extractFormDict[algo](request.POST, request.FILES) type = typeDict[algo] p = Process(target=algoDict[algo], args=(q, )) # fork process to extract if extractForm.is_valid(): p.start() q.put([request.POST, request.FILES['file'].temporary_file_path()]) try: retval = q.get(True, 10) except Q.Empty: retval = -2 p.join() if retval == -1: text += "%s-Datei nicht gefunden oder fehlerhaft."%(type) elif retval == -2: text += "Fehler beim Ausbetten. Anderes Bild oder andere Parameter versuchen." else: #print retval text += retval # empty form else: embedForm = embedFormDict[algo]() extractForm = extractFormDict[algo]() # render return render_to_response("stego_algo.html", {'algo' : algo_object, 'embedForm' : embedForm, 'extractForm' : extractForm, 'text' : text, 'algo_type' : 'Staganographie', 'manual' : manual,})
#!/usr/bin/env python from processing import Process, Queue import time def f(q): x = q.get() print "Process number %s, sleeps for %s seconds" % (x,x) time.sleep(x) print "Process number %s finished" % x q = Queue() for i in range(10): q.put(i) i = Process(target=f, args=[q]) i.start() print "main process joins on queue" i.join() print "Main Program finished"
# All other required modules here from processing import Process, Queue import time def f(queue): x = queue.get() mf.Cout("Process number %s, sleeps for %s seconds" % (x,x)) time.sleep(x) mf.Cout("Process number %s finished" % (x)) if __name__ == "__main__": mf.StopWatchStart() # Create a queue object queue = Queue() # Create 10 processes for i in range(10): # Put an item into the queue. 10 queues in total queue.put(i) # Declare the process mf.Cout("Creating process #%s" % (i)) i = Process(target = f, args=[queue]) #for a Thread: threading.Thread(target = f, args=(queue)) => Similar structure # Start the process i.start() mf.Cout("Main process joins on queue") # Block process until all items in the Queue have been gotten and processed i.join() mf.Cout("Main program finished") mf.StopWatchStop()