Beispiel #1
0
    def snmp_query(i, out):
        while True:
            time.sleep(.1)
            if out.empty():
                sys.exit()
                print "Process Number: %s" % i
            ipaddr = out.get()
            s = Snmp()
            h = HostRecord()
            h.ip = ipaddr
            h.snmp_response = s.query()
            print h
            return h
        try:
            q.putmany(ips)

        finally:
            for i in range(num_workers):
                p = Process(target=f, args=[i, q, oq])
                p.start()
            for i in range(num_workers):
                pp = Process(target=snmp_query, args=[i, oq])
                pp.start()

        print "main process joins on queue"

        p.join()
        while not oq.empty():
            print "Validated", oq.get()

        print "Main program finished"
Beispiel #2
0
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 runpool(address, number_of_processes):
    # create a single server object -- children will each inherit a copy
    server = HTTPServer(address, RequestHandler)

    # create child processes to act as workers
    for i in range(number_of_processes - 1):
        Process(target=serve_forever, args=(server, )).start()

    # main process also acts as a worker
    serve_forever(server)
Beispiel #4
0
def multiProcessTest(n, funcs):
    """Fork N processes and run a testing function in each."""
    if type(funcs) != list:
        funcs = [funcs] * n
    procs = []
    for f, args in funcs:
        procs.append(Process(target=f, args=args))
    for p in procs:
        p.start()
    for p in procs:
        p.join()
Beispiel #5
0
	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
Beispiel #6
0
 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
Beispiel #7
0
    def __init__(self, processes=None, initializer=None, initargs=()):
        self._inqueue = SimpleQueue()
        self._outqueue = SimpleQueue()
        self._taskqueue = Queue.Queue()
        self._cache = {}
        self._state = RUN

        if processes is None:
            try:
                processes = processing.cpuCount()
            except NotImplementedError:
                processes = 1

        self._pool = [
            Process(target=worker,
                    args=(self._inqueue, self._outqueue, initializer,
                          initargs)) for i in range(processes)
        ]

        for i, w in enumerate(self._pool):
            w.setName('PoolWorker-' + ':'.join(map(str, w._identity)))
            w.start()

        self._task_handler = threading.Thread(
            target=Pool._handleTasks,
            args=(self._taskqueue, self._inqueue, self._outqueue, self._pool))
        self._task_handler.setDaemon(True)
        self._task_handler._state = RUN
        self._task_handler.start()

        self._result_handler = threading.Thread(target=Pool._handleResults,
                                                args=(self._outqueue,
                                                      self._cache))
        self._result_handler.setDaemon(True)
        self._result_handler._state = RUN
        self._result_handler.start()

        self._terminate = Finalize(
            self,
            Pool._terminatePool,
            args=(self._taskqueue, self._inqueue, self._outqueue, self._cache,
                  self._pool, self._task_handler, self._result_handler),
            exitpriority=5)
Beispiel #8
0
def run_code():
    if not request.args:
        abort(400)
    pycode = request.args.get('code', '')
    if ("__class__" in pycode) or ("_module" in pycode):
        return jsonify("timed out! you have an infinite loop!")

    pysplit = pycode.splitlines()
    # print(pycode, file=sys.stderr)
    p = Process(target=exec, args=(pycode, myglobals))
    p.start()
    p.join(2)
    p.terminate()
    if p.exception:
        if p.exception == 1:
            return jsonify("no error!")
        tb = p.exception[1]
        if isinstance(p.exception[0], SyntaxError):
            return getSynTraceback(filename, pysplit, tb)
        return getTraceback(filename, pysplit, tb)
    return jsonify("timed out! you have an infinite loop!")
        # 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()
Beispiel #10
0
#-*- 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'
Beispiel #11
0
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)
while True:  # loop forever
    try:
        notifier.process_events()
        if notifier.check_events():
Beispiel #12
0
def execInSandbox(fun, *args, **kwargs):
    from processing import Pipe, Process, Condition
    (r, w) = Pipe()

    # bind worker to the 'write' side of the pipe, and to 'fun'
    def worker(*args, **kwargs):
        # TODO: catch errors, and signal parent about them...
        try:
            w.send(fun(*args, **kwargs))
        except Exception, e:
            w.send(e)

    p = Process(target=worker, args=args, kwargs=kwargs)
    p.start()
    result = r.recv()
    if type(result) is Exception: raise result
    # check whether child exited OK..    .
    return result
Beispiel #13
0
 def rpc_processTest(self, **kwargs):
     self.app.setThermo('build_pdf', 0, 'Preparo elaborazione', 10, command='init')
     p = Process(target=self.testOtherProcess, args=(self.pageLocalDocument('testOtherProcess'),), name='pippo')
     p.start()
Beispiel #14
0
def main():
    token = 'YOUR TOKEN'
    server_join = requests.get(
        'https://api.vk.com/method/groups.getLongPollServer',
        params={
            'access_token': token,
            'v': '5.101',
            'group_id': 'ID GROUP'
        }).json()['response']

    while True:
        server = requests.get(
            '{server}?act=a_check&key={key}&ts={ts}&wait=25 '.format(
                server=server_join['server'],
                key=server_join['key'],
                ts=server_join['ts'])).json()
        updates = server['updates']

        for new in updates:
            obj = new['object']
            if new['type'] == "message_new":
                from_ids = obj['from_id']
                peer_id = obj['peer_id']
                texts = obj["text"]
                text_rm = 0
                if "reply_message" in obj:
                    reply_message = obj['reply_message']
                    print(reply_message)
                    text_rm = reply_message['text']
                    id_rm = reply_message['from_id']

                    param = {
                        'access_token': token,
                        'v': '5.101',
                        'user_id': int(id_rm),
                        'fields': 'photo_id, photo_50'
                    }
                    repl = requests.get('https://api.vk.com/method/users.get',
                                        params=param).json()['response'][0]
                    reply_photo = repl['photo_50']
                    reply_name = repl['first_name'] + " " + repl['last_name']

                elif 'fwd_messages'[0] in obj:
                    reply_message = obj['fwd_messages'][0]
                    print("REPLY = " + str(reply_message))
                    text_rm = reply_message['text']
                    id_rm = reply_message['from_id']
                    param = {
                        'access_token': self.token,
                        'v': '5.101',
                        'user_id': int(id_rm),
                        'fields': 'photo_id, photo_50'
                    }
                    repl = requests.get('https://api.vk.com/method/users.get',
                                        params=param).json()['response'][0]
                    reply_photo = repl['photo_50']
                    reply_name = repl['first_name'] + " " + repl['last_name']

                if '!quote' in str(texts).lower() and text_rm != 0:
                    url = reply_photo[:-6]  # type: Any
                    reply_photo = url[-7:]
                    myUrl = str(url)
                    myFile = "image\\" + str(myUrl[-7:])
                    request.urlretrieve(myUrl, myFile)
                    photo_obrabotka = Process(reply_photo, text_rm, reply_name)
                    adress = photo_obrabotka.paint_text()
                    print(adress)
                    name_photo = url[-7:]

                    param = {
                        'access_token': token,
                        'v': '5.101',
                    }
                    file = {'photo': open(adress, 'rb')}
                    upload1 = requests.get(
                        'https://api.vk.com/method/photos.getMessagesUploadServer',
                        params=param,
                    ).json()['response']
                    urls = upload1['upload_url']
                    upload2 = requests.post(urls, files=file).json()
                    upload3 = requests.get(
                        'https://api.vk.com/method/photos.saveMessagesPhoto',
                        params={
                            'access_token': token,
                            'v': '5.101',
                            'photo': upload2['photo'],
                            'server': upload2['server'],
                            'hash': upload2['hash']
                        }).json()['response'][0]
                    upload4 = 'photo{}_{}'.format(upload3['owner_id'],
                                                  upload3['id'])
                    param = {
                        'access_token': token,
                        'v': '5.101',
                        'peer_ids': peer_id,
                        'attachment': upload4,
                        'random_id': 0
                    }
                    self_messege = requests.get(
                        'https://api.vk.com/method/messages.send',
                        params=param).json()

            server_join['ts'] = server['ts']
Beispiel #15
0
        if out.empty():
            sys.exit()
            print "Process Number: %s" % i
        ipaddr = out.get()
        s = Snmp()
        h = HostRecord()
        h.ip = ipaddr
        h.snmp_response = s.query()
        print h
        return h
try:
    q.putmany(ips)

finally:
    for i in range(num_workers):
        p = Process(target=f, args=[i,q,oq])
        p.start()
    for i in range(num_workers):
        pp = Process(target=snmp_query, args=[i,oq])
        pp.start()

print "main process joins on queue"
p.join()
#while not oq.empty():
#    print "Validated", oq.get()


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()
Beispiel #17
0
                        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)
    while True:
        if q.empty()
            sys.exit()
        #print "Process Number: %s" % i
        ip = q.get()
        ret = 
for ip in ips:
    q.put(ip)

for i in range(num_ping_workers):
    p = Process(target=ping, args=[i,q])
    p.start()

for i in range(num_snmp_workers):
    p = Process(target=f, args=[i,q])
    p.start()

print "main process joins on queue"
p.join()
print "Main Program finished"
Beispiel #18
0
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"
Beispiel #19
0
 start = 0
 end = 0
 for i in range(PLATFORMDIVISION):
     start_index.append(start)
     end = start + bids_number_pertime
     if bids_number_reminder > 0:
         end += 1
         bids_number_reminder -= 1
     end_index.append(end)
     start = end
 cur_db_list = getCursors(conn_db, n=PLATFORMDIVISION)
 print cur_db_list
 #重复白名单
 platform_id_white_list = getPlatformIdList("repeatwhiteplatform_id.txt")
 for i in range(PLATFORMDIVISION):
     j = Process(target=readSQLReturn,
                 args=[[start_index[i], end_index[i], cur_db_list[i]]])
     j.start()
 j.join()
 print "共有" + str(bids_number) + "个标."
 stringSQL = "SELECT count(*) FROM " + dstdb_info
 cur_db.execute(stringSQL)
 good_number = cur_db.fetchone()[0]
 print "在满额限制为" + str(FULLBIDPERCENT) + "的情况下, 只有" + "%.2f" % (
     100 * (float(good_number) / bids_number)) + "%的数据是可用的."
 #changeValue("./clean_date.xml","clean_date_lasttime",clean_date_thistime)
 fp.close()
 #将所有的platform_id输出到文件
 fp = open("platform_id_list_info.txt", "w")
 for platform_id in platform_id_set:
     fp.write(str(platform_id))
     fp.write("\n")