本文整理汇总了Python中diesel.Application.add_loop方法的典型用法代码示例。如果您正苦于以下问题:Python Application.add_loop方法的具体用法?Python Application.add_loop怎么用?Python Application.add_loop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类diesel.Application
的用法示例。
在下文中一共展示了Application.add_loop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_loop_keep_alive_normal_death
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
def test_loop_keep_alive_normal_death():
v = [0]
def l():
v[0] += 1
def p():
sleep(0.9)
WVPASS(v[0] > 1)
a.halt()
a = Application()
a.add_loop(Loop(l), keep_alive=True)
a.add_loop(Loop(p))
a.run()
示例2: test_loop_keep_alive_exception
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
def test_loop_keep_alive_exception():
v = [0]
def l():
v[0] += 1
a = b # exception!
def p():
sleep(0.9)
WVPASS(v[0] > 1)
a.halt()
a = Application()
a.add_loop(Loop(l), keep_alive=True)
a.add_loop(Loop(p))
a.run()
示例3: main
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
def main():
app = Application()
app.add_loop(Loop(santa))
elf_do = "meets in study"
for i in xrange(10):
app.add_loop(Loop(actor("Elf %d" % i, 'elf', elf_group, elf_do, 3, 3)))
deer_do = "delivers toys"
for name in [
'Dasher', 'Dancer', 'Prancer',
'Vixen', 'Comet', 'Cupid',
'Donner', 'Blitzen', 'Rudolph',
]:
app.add_loop(Loop(actor(name, 'deer', deer_group, deer_do, 9, 9)))
app.run()
示例4: PostgresClient
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
db = PostgresClient()
db.connect(user="user", password="pass", database="test")
t = time.time()
for x in xrange(5000):
db.simplequery("select userid, fakenum from testtable where groupid='pgtest' limit 5")
print time.time() - t
def pgtest():
db = PostgresClient()
db.connect(user="user", password="pass", database="test")
db.simplequery("select * from testtable limit 2")
db.simplequery("insert into testtable values ('pgtest', 'pgtest', 5500)")
db.simplequery("insert into testtable values ('10', 'pgtest', 5500)")
print db.simplequery("select * from testtable where groupid='10'")
db.simplequery("update testtable set fakenum=8800 where groupid='pgtest'")
db.simplequery("select userid, fakenum from testtable where groupid='pgtest' limit 5")
db.extquery_prepare("select userid, fakenum from testtable where groupid=$1 limit 5", "foobar")
print db.extquery(("pgtest",), "foobar")
print db.extquery_dict(("pgtest",), "foobar")
print db.simplequery("select userid, fakenum from testtable where groupid='pgtest' limit 5")
print db.simplequery_dict("select userid, fakenum from testtable where groupid='pgtest' limit 5")
a.add_loop(Loop(pgtest))
# a.add_loop(Loop(exttest_time))
# a.add_loop(Loop(exttest_time))
# a.add_loop(Loop(exttest_time))
# a.add_loop(Loop(exttest_time))
# a.add_loop(Loop(exttest_time))
# a.add_loop(Loop(exttest_time))
a.run()
示例5: sleep
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
sleep(10)
log.info("putting 50000 *more* things on log")
for x in xrange(50000, 100000):
q.put(x)
sleep()
def getter():
log = glog.sublog("getter", glog.info)
got = 0
while got < 100000:
try:
s = q.get(timeout=3)
except QueueTimeout:
log.warn("timeout before getting a value, retrying...")
continue
assert s == got
got += 1
if got % 10000 == 0:
log.info("up to %s received, sleeping for 0.5s" % got)
sleep(0.5)
log.info("SUCCESS! got all 100,000")
a.halt()
a = Application()
a.add_loop(Loop(putter))
a.add_loop(Loop(getter))
a.run()
示例6: thread
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
msg = thread(self.read_chat_message, "").strip()
self.input.put(msg)
@call
def chat(self):
fork(self.input_handler)
nick = self.input.get()
send("%s\r\n" % nick)
while True:
evt, data = first(until_eol=True, waits=[self.input])
if evt == "until_eol":
print data.strip()
else:
send("%s\r\n" % data)
def chat_client():
with ChatClient("localhost", 8000) as c:
c.chat()
app = Application()
if sys.argv[1] == "server":
app.add_service(Service(chat_server, 8000))
elif sys.argv[1] == "client":
app.add_loop(Loop(chat_client))
else:
print "USAGE: python %s [server|client]" % sys.argv[0]
raise SystemExit(1)
app.run()
示例7: print
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
print (r.zrange("z1", 0, -1))
print (r.zrem("z1", "two"))
print (r.zrange("z1", 0, -1))
print (r.zrevrange("z1", 0, -1))
print (r.zrem("z1", (r.zrange("z1", 0, 0))[0]))
print (r.zrange("z1", 0, -1))
print (r.zcard("z1"))
print 'done!'
a.halt()
a.add_loop(Loop(do_set))
a.run()
#########################################
## Hub, an abstraction of sub behavior, etc
class RedisSubHub(object):
def __init__(self, host='127.0.0.1', port=REDIS_PORT):
self.host = host
self.port = port
self.sub_wake_signal = uuid.uuid4().hex
self.sub_adds = []
self.sub_rms = []
self.subs = {}
def make_client(self):
client = RedisClient(self.host, self.port)
示例8: until
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
message = yield until("\r\n")
yield "you said: %s" % message
class EchoClient(Client):
@call
def echo(self, message):
yield message + "\r\n"
back = yield until("\r\n")
yield response(back)
app = Application()
def do_echos():
client = EchoClient()
yield client.connect("localhost", 8000)
t = time.time()
for x in xrange(5000):
msg = "hello, world #%s!" % x
echo_result = yield client.echo(msg)
assert echo_result.strip() == "you said: %s" % msg
print "5000 loops in %.2fs" % (time.time() - t)
app.halt()
app.add_service(Service(handle_echo, port=8000))
app.add_loop(Loop(do_echos))
app.run()
示例9: blocker
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
# vim:ts=4:sw=4:expandtab
'''Example of deferring blocking calls to threads
'''
from diesel import Application, Loop, log, thread
import time
def blocker(taskid, sleep_time):
def task():
while True:
def f():
time.sleep(sleep_time)
thread(f)
print 'yo!', time.time(), 'from %s task' % taskid
return task
a = Application()
a.add_loop(Loop(blocker('fast', 1)))
a.add_loop(Loop(blocker('slow', 10)))
a.run()
示例10: resolve_the_google
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
def resolve_the_google():
print "started resolution!"
g_ip = resolve_dns_name("www.google.com")
print "www.google.com's ip is %s" % g_ip
try:
bad_host = "www.g8asdf21oogle.com"
print "now checking %s" % bad_host
resolve_dns_name(bad_host)
except DNSResolutionError:
print "yep, it failed as expected"
else:
raise RuntimeError("The bad host resolved. That's unexpected.")
g_ip = resolve_dns_name("www.google.com")
g_ip = resolve_dns_name("www.google.com")
g_ip = resolve_dns_name("www.google.com")
g_ip = resolve_dns_name("www.google.com")
a.halt()
def stuff():
while True:
print "doing stuff!"
sleep(0.01)
a = Application()
a.add_loop(Loop(stuff))
a.add_loop(Loop(resolve_the_google))
a.run()
示例11: get_client
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
client, heads = get_client()
code, heads, body = client.request('GET', lpath, heads)
except ConnectionClosed:
pass
else:
write_file(lpath, body)
files +=1
break
def req_loop():
global links
client, heads = get_client()
log.info(path)
code, heads, body = client.request('GET', path, heads)
write_file(path, body)
links = get_links(body)
for x in xrange(CONCURRENCY):
a.add_loop(Loop(follow_loop))
a = Application()
a.add_loop(Loop(req_loop))
log = log.sublog('http-crawler', log.info)
def stop():
log.info("Fetched %s files in %.3fs with concurrency=%s" % (files, time.time() - t, CONCURRENCY))
a.halt() # stop application
t = time.time()
a.run()
示例12: not_always_busy_worker
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
import os
from diesel import Loop, fork, Application, sleep
from diesel.util.stats import CPUStats
def not_always_busy_worker():
with CPUStats() as stats:
for _ in xrange(12):
for i in xrange(10000000): # do some work to forward cpu seconds
pass
sleep(0.1) # give up control
print "cpu seconds ", stats.cpu_seconds
def spawn_busy_workers():
for _ in xrange(0,3):
fork(not_always_busy_worker)
a = Application()
a.add_loop(Loop(spawn_busy_workers), track=True)
a.run()
示例13: hi_loop
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
# vim:ts=4:sw=4:expandtab
'''Simple udp echo client.
'''
import time
from diesel import Application, UDPService, UDPLoop, send, sleep
def hi_loop():
while 1:
send("whatup?", addr='localhost', port=8013)
print time.ctime(), "sent message to server"
sleep(3)
def hi_client(data, addr):
print time.ctime(), "remote service said '%s'" % data
app = Application()
app.add_service(UDPService(hi_client, 8014))
app.add_loop(UDPLoop(hi_loop))
app.run()
示例14: gunner
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
# vim:ts=4:sw=4:expandtab
'''Example of event firing.
'''
import time
import random
from diesel import Application, Loop, sleep, fire, wait, log
def gunner():
x = 1
while True:
fire('bam', x)
x += 1
sleep()
def sieged():
t = time.time()
while True:
n = wait('bam')
if n % 10000 == 0:
log.info(n)
if n == 50000:
delt = time.time() - t
log.info("50,000 messages in %.3fs (%.1f/s)" % (delt, 50000 / delt))
a.halt()
a = Application()
log = log.sublog('fire-system', log.info)
a.add_loop(Loop(gunner))
a.add_loop(Loop(sieged))
a.run()
示例15: consumer_timeout
# 需要导入模块: from diesel import Application [as 别名]
# 或者: from diesel.Application import add_loop [as 别名]
queue.get(waiting=False)
except QueueEmpty:
pass
else:
assert False
def consumer_timeout():
try:
queue.get(timeout=0.1)
except QueueTimeout:
pass
else:
assert False
def consumer(expected):
val = queue.get()
assert expected == val, '%s != %s' % (expected, val)
if queue.is_empty:
print 'success!'
app.halt()
app.add_loop(Loop(worker))
app.add_loop(Loop(consumer_no_wait))
app.add_loop(Loop(consumer_timeout))
app.add_loop(Loop(lambda: consumer(1)))
app.add_loop(Loop(lambda: consumer(2)))
app.run()