本文整理汇总了Python中stackless.tasklet函数的典型用法代码示例。如果您正苦于以下问题:Python tasklet函数的具体用法?Python tasklet怎么用?Python tasklet使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tasklet函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_accept
def handle_accept(self):
if self.acceptChannel.balance < 0:
currentSocket, clientAddress = asyncore.dispatcher.accept(self)
currentSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# Give them the asyncore based socket, not the standard one.
currentSocket = self.wrap_accept_socket(currentSocket)
stackless.tasklet(self.acceptChannel.send)((currentSocket, clientAddress))
示例2: test_simple_channel
def test_simple_channel(self):
output = []
def print_(*args):
output.append(args)
def Sending(channel):
print_("sending")
channel.send("foo")
def Receiving(channel):
print_("receiving")
print_(channel.receive())
ch=stackless.channel()
task=stackless.tasklet(Sending)(ch)
# Note: the argument, schedule is taking is the value,
# schedule returns, not the task that runs next
#stackless.schedule(task)
stackless.schedule()
task2=stackless.tasklet(Receiving)(ch)
#stackless.schedule(task2)
stackless.schedule()
stackless.run()
assert output == [('sending',), ('receiving',), ('foo',)]
示例3: testProducerConsumer2
def testProducerConsumer2(self):
lock = RLock()
producerReady = Condition(lock)
consumerReady = Condition(lock)
def producer(n, slot):
for i in xrange(n):
with producerReady:
while slot:
producerReady.wait()
slot.append(i)
consumerReady.notify()
def consumer(n, slot, result):
for i in xrange(n):
with consumerReady:
while not slot:
consumerReady.wait()
result.append(slot[0])
del slot[:]
producerReady.notify()
theslot = []
result = []
n = 5
stackless.tasklet(producer)(n, theslot)
stackless.tasklet(consumer)(n, theslot, result)
Run()
self.assertEqual(result, range(n))
示例4: 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()
示例5: 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()
示例6: 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)
示例7: handle_accept
def handle_accept(self):
if self.acceptChannel and self.acceptChannel.balance < 0:
t = asyncore.dispatcher.accept(self)
if t is None:
return
t[0].setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
stackless.tasklet(self.acceptChannel.send)(t)
示例8: runEventLoop
def runEventLoop():
global loop_running
if not loop_running:
event.init()
event.signal(2, die)
stackless.tasklet(eventLoop)()
loop_running = True
示例9: handle_write
def handle_write(self):
if len(self.writeQueue):
channel, flags, data = self.writeQueue[0]
del self.writeQueue[0]
def asyncore_send(self, data, flags = 0):
try:
result = self.socket.send(data, flags)
return result
except stdsocket.error as why:
if why.args[0] == EWOULDBLOCK:
return 0
channel.send_exception(why.__class__, *why.args)
if why.args[0] in (ECONNRESET,
ENOTCONN,
ESHUTDOWN,
ECONNABORTED):
self.handle_close()
return 0
nbytes = asyncore_send(self, data, flags)
if channel.balance < 0:
channel.send(nbytes)
elif len(self.sendToBuffers):
data, address, channel, oldSentBytes = self.sendToBuffers[0]
sentBytes = self.socket.sendto(data, address)
totalSentBytes = oldSentBytes + sentBytes
if len(data) > sentBytes:
self.sendToBuffers[0] = (data[sentBytes:],
address,
channel,
totalSentBytes)
else:
del self.sendToBuffers[0]
stackless.tasklet(channel.send)(totalSentBytes)
示例10: 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()
示例11: 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
示例12: __init__
def __init__(self, count):
self.count = count
self.channel = stackless.channel()
self.valarr = [[ -1 for i in range(self.count) ] for k in range(self.count)]
self.pings = []
stackless.tasklet(self.handle)()
self.initboard()
示例13: handle_read
def handle_read(self):
try:
conn, addr = self.socket.accept()
except:
pass
else:
tasklet(self.handler)(conn, addr)
示例14: Run
def Run(self):
self.example = Planet(9235632, 6378000.0, 10000.0, 0.15)
return
spherical = False
import renderer
#spherical = True#False
def getV(x,y):
y = (y-90)/180.0 * math.pi
x = x/180.0 * math.pi
#print y,x
vec = math.sin(y)*math.cos(x), math.sin(y)*math.sin(x), math.cos(y)
sorrows.terrain.SetSeed(self.example.seed)
height = sorrows.terrain.fBm(8, *vec)
if height > 0.4:
color = 1,1,1
elif height < 0:
color = 0,0.2*(1.0+height),0.5+abs(height)/2
height = 0.0
else:
color = 0,height,0
if not spherical:
return (x,y,height)+color # uncomment for 2d representation
height = 1 + height*0.2
return (vec[0] *height, vec[1]*height, vec[2] * height) + color
# return x,y,self.example.GetSurfaceHeight(vec)-self.example.radius
stackless.tasklet(renderer.Render, getV)()
示例15: __init__
def __init__(self,name,circle):
self.name = name
self.circle = circle
circle.append(self)
self.channel = stackless.channel()
stackless.tasklet(self.messageLoop)()