コード例 #1
0
ファイル: test_receiver.py プロジェクト: pjz/candygram
	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)
コード例 #2
0
 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)
コード例 #3
0
ファイル: portscan_cg.py プロジェクト: likwoka/old_py_stuffs
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
コード例 #4
0
ファイル: test_functions.py プロジェクト: pjz/candygram
	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')
コード例 #5
0
 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")
コード例 #6
0
 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")
コード例 #7
0
ファイル: test_receiver.py プロジェクト: pjz/candygram
	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')
コード例 #8
0
ファイル: test_receiver.py プロジェクト: pjz/candygram
	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])
コード例 #9
0
 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])
コード例 #10
0
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()
コード例 #11
0
ファイル: wxprocess.py プロジェクト: pjz/candygram
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()
コード例 #12
0
ファイル: test_receiver.py プロジェクト: pjz/candygram
	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')
コード例 #13
0
 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")
コード例 #14
0
ファイル: test_receiver.py プロジェクト: pjz/candygram
	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')
コード例 #15
0
ファイル: wxprocess.py プロジェクト: pjz/candygram
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
コード例 #16
0
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
コード例 #17
0
 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")
コード例 #18
0
ファイル: recipe-302323.py プロジェクト: bhramoss/code
 def sendVal(self, msg):
     """Send current value of counter to requesting thread."""
     req = msg[0]
     req.send((cg.self(), self.val))
コード例 #19
0
ファイル: recipe-302323.py プロジェクト: bhramoss/code
        # 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')
コード例 #20
0
 def sendVal(self, msg):
     """ 将当前counter值发给询问的线程 """
     req = msg[0]
     req.send((cg.self(), self.val))
コード例 #21
0
ファイル: cook_9.py プロジェクト: so-easy/python-cookbook-1
 def sendVal(self, msg):
     """ 将当前counter值发给询问的线程 """
     req = msg[0]
     req.send((cg.self(), self.val))
コード例 #22
0
ファイル: recipe-302323.py プロジェクト: zlrs/code-1
        # 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')
コード例 #23
0
ファイル: recipe-302323.py プロジェクト: zlrs/code-1
 def sendVal(self, msg):
     """Send current value of counter to requesting thread."""
     req = msg[0]
     req.send((cg.self(), self.val))
コード例 #24
0
ファイル: test_patterns.py プロジェクト: nopdotcom/candygram
 def assertNonMatch(self, value):
     cg.self().send(value)
     assert self.r.receive() == "non-match"
コード例 #25
0
ファイル: test_patterns.py プロジェクト: nopdotcom/candygram
 def assertMatch(self, value):
     cg.self().send(value)
     assert self.r.receive() == "match"