def testRemoveHandler(self): r = cg.Receiver() ref1 = r.addHandler(1) ref2 = r.addHandler(2) ref3 = r.addHandler(3) self.assert_(len(r._Receiver__handlers), 3) cg.self().send(2) self.assertEqual(r.receive(0, lambda: 'to'), None) r.removeHandler(ref2) self.assert_(len(r._Receiver__handlers), 2) cg.self().send(2) self.assertEqual(r.receive(0, lambda: 'to'), 'to') self.assertRaises(cg.ExitError, r.removeHandler, ref2)
def testRemoveHandler(self): r = cg.Receiver() ref1 = r.addHandler(1) ref2 = r.addHandler(2) ref3 = r.addHandler(3) assert len(r._Receiver__handlers), 3 cg.self().send(2) assert r.receive(0, lambda: "to") == None r.removeHandler(ref2) assert len(r._Receiver__handlers), 2 cg.self().send(2) assert r.receive(0, lambda: "to") == "to" with pytest.raises(cg.ExitError): r.removeHandler(ref2)
def main(argv): if len(argv) != 4: print "Usage: portscan_cg.py localhost 1 80" return 1 host = argv[1] start = int(argv[2]) end = int(argv[3]) r = cg.Receiver() r[Result] = lambda m: m, cg.Message scanners = [cg.spawn(scan) for i in range(0, NUM_OF_WORKERS)] i = 0 for port in range(start, end + 1): scanners[i] | (cg.self(), Target(host, port)) i += 1 if i == NUM_OF_WORKERS: i = 0 port_count = end - start + 1 ports = [] j = 0 while j != port_count: result = r.receive() j += 1 if result.is_success: ports.append(result.port) ports.sort() for port in ports: print "Connected to port %s" % port return 0
def testSpawnLinkMyProcess(self): proc = cg.spawnLink(echo, cg.self(), _processClass=MyProcess) self.assert_(isinstance(proc, MyProcess)) proc.send('test') r = cg.Receiver() r.addHandler(cg.Any, lambda m: m, cg.Message) self.assertEqual(r.receive(), ('MyProcess', 'test')) cg.exit(proc, 'kill')
def testSpawnMyProcess(self): proc = cg.spawn(echo, cg.self(), _processClass=MyProcess) assert isinstance(proc, MyProcess) proc.send("test") r = cg.Receiver() r.addHandler(cg.Any, lambda m: m, cg.Message) assert r.receive() == ("MyProcess", "test") cg.exit(proc, "kill")
def testSingleProc(self): r = cg.Receiver() r.addHandler(1, cg.self().send, "one") proc = cg.spawn(self.receive, r) proc.send(1) r = cg.Receiver() r.addHandler("one", lambda: "success") assert r.receive(1000) == "success" cg.exit(proc, "kill")
def testSingleProc(self): r = cg.Receiver() r.addHandler(1, cg.self().send, 'one') proc = cg.spawn(self.receive, r) proc.send(1) r = cg.Receiver() r.addHandler('one', lambda: 'success') self.assertEqual(r.receive(1000), 'success') cg.exit(proc, 'kill')
def testRemoveHandlers(self): r1 = cg.Receiver() ref1 = r1.addHandler(1) ref2 = r1.addHandler(2) ref3 = r1.addHandler(3) r2 = cg.Receiver() r2.addHandler(4) r2.addHandler(5) r2.addHandler(6) refs = r2.addHandlers(r1) self.assert_(len(r2._Receiver__handlers), 6) cg.self().send(2) self.assertEqual(r2.receive(0, lambda: 'to'), None) r2.removeHandler(refs[1]) self.assert_(len(r2._Receiver__handlers), 5) cg.self().send(2) self.assertEqual(r2.receive(0, lambda: 'to'), 'to') self.assertRaises(cg.ExitError, r2.removeHandler, refs[1])
def testRemoveHandlers(self): r1 = cg.Receiver() ref1 = r1.addHandler(1) ref2 = r1.addHandler(2) ref3 = r1.addHandler(3) r2 = cg.Receiver() r2.addHandler(4) r2.addHandler(5) r2.addHandler(6) refs = r2.addHandlers(r1) assert len(r2._Receiver__handlers), 6 cg.self().send(2) assert r2.receive(0, lambda: "to") == None r2.removeHandler(refs[1]) assert len(r2._Receiver__handlers), 5 cg.self().send(2) assert r2.receive(0, lambda: "to") == "to" with pytest.raises(cg.ExitError): r2.removeHandler(refs[1])
def mainLoop(proc, ack): """Create a wx.PySimpleApp and run its MainLoop()""" app = wx.PySimpleApp() proc.send(ack) # We can define our Candygram handler functions here: r = cg.self().receiver # r.addHandler(...) # r.addHandler(...) # ... and so on ... app.MainLoop()
def mainLoop(proc, ack): """Create a wx.PySimpleApp and run its MainLoop()""" app = wx.PySimpleApp() proc.send(ack) # We can define our Candygram handler functions here: r = cg.self().receiver # r.addHandler(...) # r.addHandler(...) # ... and so on ... app.MainLoop()
def testReset(self): # test that Receiver resets pointer back to front of mailbox after switching # to another process r1 = cg.Receiver() r1.addHandler(1, cg.self().send, 'one') proc1 = cg.spawn(self.receive, r1) proc1.send(2) proc1.send(1) r = cg.Receiver() r.addHandler('one', lambda: 'success') self.assertEqual(r.receive(1000), 'success') proc2 = cg.spawn(self.receive, r1) proc2.send(1) self.assertEqual(r.receive(1000), 'success') cg.exit(proc1, 'kill') cg.exit(proc2, 'kill')
def testReset(self): # test that Receiver resets pointer back to front of mailbox after switching # to another process r1 = cg.Receiver() r1.addHandler(1, cg.self().send, "one") proc1 = cg.spawn(self.receive, r1) proc1.send(2) proc1.send(1) r = cg.Receiver() r.addHandler("one", lambda: "success") assert r.receive(1000) == "success" proc2 = cg.spawn(self.receive, r1) proc2.send(1) assert r.receive(1000) == "success" cg.exit(proc1, "kill") cg.exit(proc2, "kill")
def testMultiProc(self): r1 = cg.Receiver() r1.addHandler(1, cg.self().send, 'one') proc1 = cg.spawn(self.receive, r1) proc2 = cg.spawn(self.receive, r1) proc1.send(1) proc2.send(1) r = cg.Receiver() r.addHandler('one', lambda: 'success') self.assertEqual(r.receive(1000), 'success') self.assertEqual(r.receive(1000), 'success') proc3 = cg.spawn(self.receive, r1) proc3.send(1) self.assertEqual(r.receive(1000), 'success') cg.exit(proc1, 'kill') cg.exit(proc2, 'kill') cg.exit(proc3, 'kill')
def sampleSpawn(): """Start GUI event loop in separate process""" # Unique value used to acknowledge that the wx.App has been created: ack = object() # Unique value used to signal a receive() timeout: timeout = object() # Spawn a new wxProcess, calling mainLoop(): proc = cg.spawn(mainLoop, cg.self(), ack, _processClass=wxProcess) # We wait for acknowledgement from the spawned process before proceeding, # since we should make sure that the wx.App has been instantiated before # allowing anyone to send messages to the process. r = cg.Receiver() r.addHandler(ack) # Wait at most 30 seconds. result = r.receive(30000, lambda: timeout) assert result is not timeout return proc
def sampleSpawn(): """Start GUI event loop in separate process""" # Unique value used to acknowledge that the wx.App has been created: ack = object() # Unique value used to signal a receive() timeout: timeout = object() # Spawn a new wxProcess, calling mainLoop(): proc = cg.spawn(mainLoop, cg.self(), ack, _processClass=wxProcess) # We wait for acknowledgement from the spawned process before proceeding, # since we should make sure that the wx.App has been instantiated before # allowing anyone to send messages to the process. r = cg.Receiver() r.addHandler(ack) # Wait at most 30 seconds. result = r.receive(30000, lambda: timeout) assert result is not timeout return proc
def testMultiProc(self): r1 = cg.Receiver() r1.addHandler(1, cg.self().send, "one") proc1 = cg.spawn(self.receive, r1) proc2 = cg.spawn(self.receive, r1) proc1.send(1) proc2.send(1) r = cg.Receiver() r.addHandler("one", lambda: "success") assert r.receive(1000) == "success" assert r.receive(1000) == "success" proc3 = cg.spawn(self.receive, r1) proc3.send(1) assert r.receive(1000) == "success" cg.exit(proc1, "kill") cg.exit(proc2, "kill") cg.exit(proc3, "kill")
def sendVal(self, msg): """Send current value of counter to requesting thread.""" req = msg[0] req.send((cg.self(), self.val))
# Register the handler functions for various messages: r = cg.Receiver() r.addHandler('increment', self.increment) r.addHandler((cg.Process, 'value'), self.sendVal, cg.Message) r.addHandler('stop', self.setStop) # Keep handling new messages until the stop flag is set. while not self.stop: r.receive() # end while # Create new thread. counter = cg.spawn(Thread().run) # Define a receiver that will return the thread's response values: response = cg.Receiver() response.addHandler((counter, int), lambda msg: msg[1], cg.Message) # Tell thread to increment twice. counter.send('increment') counter.send('increment') # Request thread's current value. counter.send((cg.self(), 'value')) # Print the response print response.receive() # Tell thread to increment one more time. counter.send('increment') # And print it's current value. counter.send((cg.self(), 'value')) print response.receive() # Tell thread to stop. counter.send('stop')
def sendVal(self, msg): """ 将当前counter值发给询问的线程 """ req = msg[0] req.send((cg.self(), self.val))
def sendVal(self, msg): """ 将当前counter值发给询问的线程 """ req = msg[0] req.send((cg.self(), self.val))
# Register the handler functions for various messages: r = cg.Receiver() r.addHandler('increment', self.increment) r.addHandler((cg.Process, 'value'), self.sendVal, cg.Message) r.addHandler('stop', self.setStop) # Keep handling new messages until the stop flag is set. while not self.stop: r.receive() # end while # Create new thread. counter = cg.spawn(Thread().run) # Define a receiver that will return the thread's response values: response = cg.Receiver() response.addHandler((counter, int), lambda msg: msg[1], cg.Message) # Tell thread to increment twice. counter.send('increment') counter.send('increment') # Request thread's current value. counter.send((cg.self(), 'value')) # Print the response print response.receive() # Tell thread to increment one more time. counter.send('increment') # And print it's current value. counter.send((cg.self(), 'value')) print response.receive() # Tell thread to stop. counter.send('stop')
def sendVal(self, msg): """Send current value of counter to requesting thread.""" req = msg[0] req.send((cg.self(), self.val))
def assertNonMatch(self, value): cg.self().send(value) assert self.r.receive() == "non-match"
def assertMatch(self, value): cg.self().send(value) assert self.r.receive() == "match"