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 cmdrun(self, cmd): comScanCmd = cmd queue = Queue() scanProc = Process( target=self.newProcExecuteCmd, args=[queue, comScanCmd]) scanProc.start() # 等待5秒 scanProc.join(10) try: scanResult = queue.get(timeout=5) except Exception as e: print "get cmd result error" scanResult = -1 scanProc.terminate() return scanResult
def cmdrun(self, cmd): try: comScanCmd = cmd queue = Queue() scanProc = Process(target=self.newProcExecuteCmd, args=[queue, comScanCmd]) scanProc.start() scanProc.join(5) try: scanResult = queue.get(timeout=30) #print scanResult except Exception, e: print e print "get cmd result error: %s " % str(e) scanResult = -1 scanProc.terminate() return scanResult
from processing import Process, Queue, Pool import time 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):
#!/usr/bin/env python from processing import Process, Queue, Pool import time import subprocess from IPy import IP import sys from snmp import Snmp q = Queue() oq = Queue() #ips = IP("10.0.1.0/24") ips = ["192.19.101.250", "192.19.101.251", "192.19.101.252","192.19.101.253", "192.168.1.1"] num_workers = 10 class HostRecord(object): """Record for Hosts""" def __init__(self, ip=None, mac=None, snmp_response=None): self.ip = ip self.mac = mac self.snmp_response = snmp_response def __repr__(self): return "[Host Record('%s','%s','%s')]" % (self.ip, self.mac, self.snmp_response) def f(i,q,oq): while True: time.sleep(.1) if q.empty(): sys.exit()
python_pingSweep_threading.py, but relies on processing instead of threading. ''' # Import my own modules here import python_myFunctions as myFunctions mf = myFunctions.CreateObject() # All other required modules here from processing import Process, Queue, Pool import time import subprocess from IPy import IP #sudo easy_install IPy import sys # Declarations here queue = Queue() # Create an instance of an IP object. If no size specification is given a size of 1 address (/32 for IPv4 and /128 for IPv6) is assumed. #ipAddresses = IP("10.0.1.0/24") #generates 10.0.1.0 -> 10.0.1.255 ipAddresses = ["172.20.43.175", "194.42.7.189", "194.42.7.57", "127.0.0.1"] nProcesses = 2 def f(iProcess, queue): # Create an infinite loop! while True: # Place a conditional statement for exiting the loop if queue.empty(): mf.Cout("Queue for Process #%s is empty. Exiting python shell." % (iProcess)) #print __doc__ sys.exit(1)
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()
if len(message["match"]) > 0 or message.has_key("host") or message.has_key("smtpresp"): sendmsg(channel, json.dumps(message)) except Exception, e: print e def worker(inp, outp): for arg in iter(inp.get, 'STOP'): try: rez = dopcap(arg) os.unlink(arg) except Exception, e: print e print "Done: %s" % arg task_queue = Queue() done_queue = Queue() for i in range(PROCS): Process(target=worker, args=(task_queue, done_queue)).start() class CloseEvent(ProcessEvent): def process_IN_CLOSE_WRITE(self, event): task_queue.put("%s" % os.path.join(event.path, event.name)) print "Received: %s" % os.path.join(event.path, event.name) wm = WatchManager() notifier = Notifier(wm, CloseEvent()) wdd = wm.add_watch('/data2', pyinotify.IN_CLOSE_WRITE, rec=True)
#!/usr/bin/env python from processing import Process, Queue, Pool import time import subprocess from IPy import IP import sys q = Queue() ping_out_queue = Queue() snmp_out_queue = Queue() ips = IP("10.0.1.0/24") num_ping_workers = 10 num_snmp_workers = 10 def ping(i,q,out=out_queue): 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 out.put(ip) else: pass def snmp(i,q=ping_out_queue,out=snmp_out_queue)