本文整理汇总了Python中stackless.run函数的典型用法代码示例。如果您正苦于以下问题:Python run函数的具体用法?Python run怎么用?Python run使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了run函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_except
def test_except(self):
rlist = []
def f():
rlist.append('f')
return 1/0
def g():
rlist.append('bg')
stackless.schedule()
rlist.append('ag')
def h():
rlist.append('bh')
stackless.schedule()
rlist.append('ah')
tg = stackless.tasklet(g)()
tf = stackless.tasklet(f)()
th = stackless.tasklet(h)()
try:
stackless.run()
# cheating, can't test for ZeroDivisionError
except Exception as e:
rlist.append('E')
stackless.schedule()
stackless.schedule()
assert rlist == "bg f E bh ag ah".split()
示例2: TestMonkeyPatchUDP
def TestMonkeyPatchUDP(address):
install()
try:
def UDPServer(address):
listenSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
listenSocket.bind(address)
print 'waiting to receive'
rdata = ''
while len(rdata) < 512:
data, address = listenSocket.recvfrom(4096)
print 'received', data, len(data)
rdata += data
def UDPClient(address):
clientSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
print 'sending 512 byte packet'
sentBytes = clientSocket.sendto('-' + '*' * 510 + '-', address)
print 'sent 512 byte packet', sentBytes
stackless.tasklet(UDPServer)(address)
stackless.tasklet(UDPClient)(address)
stackless.run()
finally:
uninstall()
示例3: mainloop
def mainloop():
if REACTOR_RUN_NORMAL:
ftlog.info('Main loop begin. REACTOR_RUN_NORMAL')
stackless.tasklet(reactor.run)()
reactor.callLater(0, stackless.schedule)
stackless.run()
ftlog.info('Main loop over. REACTOR_RUN_NORMAL')
return
loopcount = 0 # 测试代码, 有时接到tcp消息, 却不处理, log也不出, 为何?
r = reactor # escape eclipse error
r.startRunning()
while r._started:
try:
while r._started:
loopcount += 1
if loopcount > 1000000000:
loopcount = 0
if loopcount % 100 == 0:
ftlog.debug("Main loop 100!")
# Advance simulation time in delayed event processors.
r.runUntilCurrent()
_schedule_reactor()
t2 = r.timeout()
t = r.running and t2
r.doIteration(t)
_schedule_reactor()
except:
ftlog.error("Main loop error!")
else:
ftlog.info('Main loop terminated.')
finally:
_schedule_reactor()
ftlog.info('Main loop over.')
sys.exit(0)
示例4: lifecycle
def lifecycle(self, t):
# Initial state - unrun
self.assertTrue(t.alive)
self.assertTrue(t.scheduled)
self.assertEqual(t.recursion_depth, 0)
# allow hard switching
t.set_ignore_nesting(1)
softSwitching = stackless.enable_softswitch(0); stackless.enable_softswitch(softSwitching)
# Run a little
res = stackless.run(10)
self.assertEqual(t, res)
self.assertTrue(t.alive)
self.assertTrue(t.paused)
self.assertFalse(t.scheduled)
self.assertEqual(t.recursion_depth, softSwitching and 1 or 2)
# Push back onto queue
t.insert()
self.assertFalse(t.paused)
self.assertTrue(t.scheduled)
# Run to completion
stackless.run()
self.assertFalse(t.alive)
self.assertFalse(t.scheduled)
self.assertEqual(t.recursion_depth, 0)
示例5: switch
def switch(self, data=None):
if not self._tasklet.scheduled:
self._tasklet.insert()
stackless.run()
self._channel.send(data)
return self._channel.receive()
示例6: TestMonkeyPatchUDP
def TestMonkeyPatchUDP(address):
# replace the system socket with this module
install()
try:
def UDPServer(address):
listenSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
listenSocket.bind(address)
# Apparently each call to recvfrom maps to an incoming
# packet and if we only ask for part of that packet, the
# rest is lost. We really need a proper unittest suite
# which tests this module against the normal socket
# module.
print "waiting to receive"
rdata = ""
while len(rdata) < 512:
data, address = listenSocket.recvfrom(4096)
print "received", data, len(data)
rdata += data
def UDPClient(address):
clientSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
# clientSocket.connect(address)
print "sending 512 byte packet"
sentBytes = clientSocket.sendto("-"+ ("*" * 510) +"-", address)
print "sent 512 byte packet", sentBytes
stackless.tasklet(UDPServer)(address)
stackless.tasklet(UDPClient)(address)
stackless.run()
finally:
uninstall()
示例7: lifecycle
def lifecycle(self, t):
# Initial state - unrun
self.assert_(t.alive)
self.assert_(t.scheduled)
self.assertEquals(t.recursion_depth, 0)
# allow hard switching
t.set_ignore_nesting(1)
# Run a little
res = stackless.run(10)
self.assertEquals(t, res)
self.assert_(t.alive)
self.assert_(t.paused)
self.failIf(t.scheduled)
self.assertEquals(t.recursion_depth, 1)
# Push back onto queue
t.insert()
self.failIf(t.paused)
self.assert_(t.scheduled)
# Run to completion
stackless.run()
self.failIf(t.alive)
self.failIf(t.scheduled)
self.assertEquals(t.recursion_depth, 0)
示例8: test_bind
def test_bind(self):
t = stackless.tasklet()
wr = weakref.ref(t)
self.assertFalse(t.alive)
self.assertIsNone(t.frame)
self.assertEquals(t.nesting_level, 0)
t.bind(None) # must not change the tasklet
self.assertFalse(t.alive)
self.assertIsNone(t.frame)
self.assertEquals(t.nesting_level, 0)
t.bind(self.task)
t.setup(False)
stackless.run()
self.assertFalse(t.scheduled)
self.assertTrue(t.alive)
if stackless.enable_softswitch(None):
self.assertTrue(t.restorable)
self.assertIsInstance(t.frame, types.FrameType)
t.insert()
stackless.run()
# remove the tasklet. Must run the finally clause
t = None
self.assertIsNone(wr()) # tasklet has been deleted
self.assertEqual(self.finally_run_count, 1)
示例9: test_scheduling_cleanup
def test_scheduling_cleanup(self):
rlist = []
def f():
rlist.append('fb')
stackless.schedule()
rlist.append('fa')
def g():
rlist.append('gb')
stackless.schedule()
rlist.append('ga')
def h():
rlist.append('hb')
stackless.schedule()
rlist.append('ha')
tf = stackless.tasklet(f)()
tg = stackless.tasklet(g)()
th = stackless.tasklet(h)()
rlist.append('mb')
stackless.run()
rlist.append('ma')
assert rlist == 'mb fb gb hb fa ga ha ma'.split()
示例10: test_with_channel
def test_with_channel(self):
rlist = []
def f(outchan):
for i in range(10):
rlist.append('s%s' % i)
outchan.send(i)
outchan.send(-1)
def g(inchan):
while 1:
val = inchan.receive()
if val == -1:
break
rlist.append('r%s' % val)
ch = stackless.channel()
t1 = stackless.tasklet(f)(ch)
t2 = stackless.tasklet(g)(ch)
stackless.run()
assert len(rlist) == 20
for i in range(10):
(s,r), rlist = rlist[:2], rlist[2:]
assert s == 's%s' % i
assert r == 'r%s' % i
示例11: testSocketIsManaged
def testSocketIsManaged(self):
"""The goal of this test is to verify that the tasklet created to
manage the socket, actually starts."""
# Give the rpc module access to the 'tasklet' class as its stackless
# module has been replaced with a mock object.
rpc.stackless.tasklet = stackless.tasklet
# Make a mock version of '_ManageSocket' we can tell has been called.
def new_ManageSocket(self):
new_ManageSocket.called = True
new_ManageSocket.called = False
# Inject it in a way where we restore the old version so that
# subsequent tests are not clobbered by leakage.
old_ManageSocket = rpc.EndPoint._ManageSocket
rpc.EndPoint._ManageSocket = new_ManageSocket
try:
_socket = DummyClass()
endpoint = rpc.EndPoint(_socket)
self.failUnless(stackless.runcount == 2, "_ManageSocket was not launched in a tasklet")
# Ensure the _ManageSocket tasklet starts.
stackless.run()
finally:
rpc.EndPoint._ManageSocket = old_ManageSocket
# Verify that the scheduler/tasklet state is correct.
self.failUnless(stackless.runcount == 1, "The scheduler still contains tasklets")
# Verify that the tasklet gets started.
self.failUnless(new_ManageSocket.called, "The mock _ManageSocket function was not called")
示例12: TestMonkeyPatchUDP
def TestMonkeyPatchUDP(address):
# replace the system socket with this module
#oldSocket = sys.modules["socket"]
#sys.modules["socket"] = __import__(__name__)
install()
try:
def UDPServer(address):
listenSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
listenSocket.bind(address)
# Apparently each call to recvfrom maps to an incoming
# packet and if we only ask for part of that packet, the
# rest is lost. We really need a proper unittest suite
# which tests this module against the normal socket
# module.
print "waiting to receive"
data, address = listenSocket.recvfrom(256)
print "received", data, len(data)
if len(data) != 256:
raise StandardError("Unexpected UDP packet size")
def UDPClient(address):
clientSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
# clientSocket.connect(address)
print "sending 512 byte packet"
sentBytes = clientSocket.sendto("-"+ ("*" * 510) +"-", address)
print "sent 512 byte packet", sentBytes
stackless.tasklet(UDPServer)(address)
stackless.tasklet(UDPClient)(address)
stackless.run()
finally:
#sys.modules["socket"] = oldSocket
uninstall()
示例13: mainloop
def mainloop():
tylog.info('Main loop begin.')
stackless.tasklet(reactor.run)()
reactor.callLater(0, stackless.schedule)
stackless.run()
tylog.info('Main loop over.')
sys.exit(0)
示例14: test_except_full
def test_except_full(self):
rlist = []
def f():
rlist.append('f')
return 1/0
def g():
rlist.append('bg')
stackless.schedule()
rlist.append('ag')
def h():
rlist.append('bh')
stackless.schedule()
rlist.append('ah')
tg = stackless.tasklet(g)()
tf = stackless.tasklet(f)()
th = stackless.tasklet(h)()
try:
stackless.run()
except ZeroDivisionError:
rlist.append('E')
stackless.schedule()
stackless.schedule()
assert rlist == "bg f E bh ag ah".split()
示例15: _test_watchdog_priority
def _test_watchdog_priority(self, soft):
self.awoken = 0
def runner_func(recursive, start):
if recursive:
stackless.tasklet(runner_func)(recursive-1, start)
with stackless.atomic():
stackless.run(2, soft=soft, totaltimeout=True, ignore_nesting=True)
a = self.awoken
self.awoken += 1
if recursive == start:
# we are the first watchdog
self.assertEqual(a, 0) #the first to wake up
self.done += 1 # we were interrupted
t1.kill()
t2.kill()
def task():
while True:
for i in xrange(100):
i = i
stackless.schedule()
t1 = stackless.tasklet(task)()
t2 = stackless.tasklet(task)()
t3 = stackless.tasklet(runner_func)(3, 3)
stackless.run()
self.assertEqual(self.done, 2)