本文整理汇总了Python中twisted.internet.selectreactor.SelectReactor.callLater方法的典型用法代码示例。如果您正苦于以下问题:Python SelectReactor.callLater方法的具体用法?Python SelectReactor.callLater怎么用?Python SelectReactor.callLater使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.selectreactor.SelectReactor
的用法示例。
在下文中一共展示了SelectReactor.callLater方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_hello
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
def test_hello(self):
def _check(lc, reactor):
if "published to 'oncounter'" in self.stdout.getvalue():
lc.stop()
try:
reactor.stop()
except:
pass
appdir = self.mktemp()
cbdir = os.path.join(appdir, ".crossbar")
reactor = SelectReactor()
cli.run("crossbar", ["init", "--appdir={}".format(appdir), "--template=hello:python"], reactor=reactor)
self.assertIn("Application template initialized", self.stdout.getvalue())
reactor = SelectReactor()
make_lc(self, reactor, _check)
# In case it hard-locks
reactor.callLater(self._subprocess_timeout, reactor.stop)
cli.run("crossbar", ["start", "--cbdir={}".format(cbdir.path), "--logformat=syslogd"], reactor=reactor)
stdout_expected = ["published to 'oncounter'"]
for i in stdout_expected:
self.assertIn(i, self.stdout.getvalue())
示例2: _start_run
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
def _start_run(self, config, app, stdout_expected, stderr_expected,
end_on):
with open(self.config, "wb") as f:
f.write(json.dumps(config, ensure_ascii=False).encode('utf8'))
with open(self.code_location + "/myapp.py", "w") as f:
f.write(app)
reactor = SelectReactor()
make_lc(self, reactor, end_on)
# In case it hard-locks
reactor.callLater(self._subprocess_timeout, reactor.stop)
cli.run("crossbar",
["start",
"--cbdir={}".format(self.cbdir),
"--logformat=syslogd"],
reactor=reactor)
for i in stdout_expected:
self.assertIn(i, self.stdout.getvalue())
for i in stderr_expected:
self.assertIn(i, self.stderr.getvalue())
示例3: _start_run
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
def _start_run(self, config, app, stdout_expected, stderr_expected, end_on):
with open(self.config, "wb") as f:
f.write(json.dumps(config, ensure_ascii=False).encode("utf8"))
with open(self.code_location + "/myapp.py", "w") as f:
f.write(app)
reactor = SelectReactor()
make_lc(self, reactor, end_on)
# In case it hard-locks
reactor.callLater(self._subprocess_timeout, reactor.stop)
cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logformat=syslogd"], reactor=reactor)
out = self.stdout.getvalue()
err = self.stderr.getvalue()
for i in stdout_expected:
if i not in out:
self.fail(u"Error: '{}' not in:\n{}".format(i, out))
for i in stderr_expected:
if i not in err:
self.fail(u"Error: '{}' not in:\n{}".format(i, err))
示例4: test_basic_subresources
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
def test_basic_subresources(self):
"""
A basic WSGI app can be ran, with subresources
"""
temp_reactor = SelectReactor()
r = router.RouterWorkerSession(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
r.start_router_realm(u"realm1", realm_config)
r.start_router_transport(
"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"/": {
"module": u"crossbar.worker.test.test_router",
"object": u"hello",
"type": u"wsgi"
},
u"json": {
"type": u"json",
"value": {}
}
}
})
# Make a request to the /json endpoint, which is technically a child of
# the WSGI app, but is not served by WSGI.
d = treq.get("http://localhost:8080/json", reactor=temp_reactor)
d.addCallback(treq.content)
d.addCallback(self.assertEqual, b"{}")
d.addCallback(lambda _: temp_reactor.stop())
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()
return d
示例5: test_basic
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
def test_basic(self):
"""
A basic WSGI app can be ran.
"""
temp_reactor = SelectReactor()
r = router.RouterController(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
r.start_router_realm(u"realm1", realm_config)
r.start_router_transport(
u"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"/": {
"module": u"crossbar.worker.test.test_router",
"object": u"hello",
"type": u"wsgi"
}
}
})
# Make a request to the WSGI app.
d = treq.get("http://localhost:8080/", reactor=temp_reactor)
d.addCallback(treq.content)
d.addCallback(self.assertEqual, b"hello!")
d.addCallback(lambda _: temp_reactor.stop())
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()
return d
示例6: ReactorSlaveController
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
class ReactorSlaveController(object):
def __init__(self):
self.keepGoing = True
self.reactor = SelectReactor()
installReactor(self.reactor)
connection = self.reactor.connectTCP('localhost', 8000, factory)
self.reactor.startRunning()
self.futureCall = None
self.futureCallTimeout = None
pygame_test.prepare()
def iterate(self):
print 'in iterate'
self.reactor.runUntilCurrent()
self.reactor.doIteration(0)
#t2 = self.reactor.timeout()
#print 'timeout', t2
#t = self.reactor.running and t2
#self.reactor.doIteration(t)
def run(self):
clock = pygame.time.Clock()
self.reactor.callLater(20, stupidTest)
while self.keepGoing:
timeChange = clock.tick(FRAMES_PER_SECOND)
if self.futureCall:
self.futureCallTimeout -= timeChange
print 'future call in', self.futureCallTimeout
if self.futureCallTimeout <= 0:
self.futureCall()
self.futureCallTimeout = None
self.futureCall= None
retval = pygame_test.iterate()
if retval == False:
thingInControl.stop()
self.iterate()
def stop(self):
print 'stopping'
self.reactor.stop()
self.keepGoing = False
def callLater(self, when, fn):
self.futureCallTimeout = when*1000
self.futureCall = fn
print 'future call in', self.futureCallTimeout
示例7: test_root_not_required
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
def test_root_not_required(self):
"""
Not including a '/' path will mean that path has a 404, but children
will still be routed correctly.
"""
temp_reactor = SelectReactor()
r = router.RouterWorkerSession(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
# Make a file
with open(os.path.join(self.cbdir, 'file.txt'), "wb") as f:
f.write(b"hello!")
r.start_router_realm(u"realm1", realm_config)
r.start_router_transport(
u"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"static": {
u"directory": u".",
u"type": u"static"
}
}
})
d1 = treq.get("http://localhost:8080/", reactor=temp_reactor)
d1.addCallback(lambda resp: self.assertEqual(resp.code, 404))
d2 = treq.get("http://localhost:8080/static/file.txt",
reactor=temp_reactor)
d2.addCallback(treq.content)
d2.addCallback(self.assertEqual, b"hello!")
def done(results):
for item in results:
if not item[0]:
return item[1]
d = defer.DeferredList([d1, d2])
d.addCallback(done)
d.addCallback(lambda _: temp_reactor.stop())
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()
示例8: _test_start_run
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
#.........这里部分代码省略.........
"name": "anonymous",
"permissions": [
{
"uri": "*",
"publish": true,
"subscribe": true,
"call": true,
"register": true
}
]
}
]
}
],
"transports": [
{
"type": "web",
"endpoint": {
"type": "tcp",
"port": 8080
},
"paths": {
"/": {
"directory": ".",
"type": "static"
},
"ws": {
"type": "websocket"
}
}
}
]
},
{
"type": "container",
"options": {
"pythonpath": ["%s"]
},
"components": [
{
"type": "class",
"classname": "test.AppSession",
"realm": "realm1",
"transport": {
"type": "websocket",
"endpoint": {
"type": "tcp",
"host": "127.0.0.1",
"port": 8080
},
"url": "ws://127.0.0.1:8080/ws"
}
}
]
}
]
}
""" % ("/".join(code_location.split(os.sep),)))
with open(code_location + "/test.py", "w") as f:
f.write("""#!/usr/bin/env python
from twisted.internet.defer import inlineCallbacks
from twisted.logger import Logger
from autobahn.twisted.wamp import ApplicationSession
from autobahn.wamp.exception import ApplicationError
class AppSession(ApplicationSession):
log = Logger()
@inlineCallbacks
def onJoin(self, details):
self.log.info("Loaded the component!")
yield self.publish("com.bar", "test")
""")
reactor = SelectReactor()
def _check(lc):
if "Loaded the component!" in self.stdout.getvalue():
if reactor.running:
reactor.stop()
lc.stop()
lc = LoopingCall(_check)
lc.a = (lc,)
lc.clock = reactor
# In case it hard-locks
reactor.callLater(self._subprocess_timeout, reactor.stop)
lc.start(0.1)
cli.run("crossbar",
["start",
"--cbdir={}".format(self.cbdir),
"--logformat=syslogd"],
reactor=reactor)
self.assertIn("Entering reactor event loop", self.stdout.getvalue())
self.assertIn("Loaded the component!", self.stdout.getvalue())
示例9: test_threads
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import callLater [as 别名]
def test_threads(self):
"""
A basic WSGI app can be ran, with subresources
"""
temp_reactor = SelectReactor()
r = router.RouterWorkerSession(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
threads = 20
r.start_router_realm("realm1", realm_config)
r.start_router_transport(
"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"/": {
"module": u"crossbar.worker.test.test_router",
"object": u"sleep",
"type": u"wsgi",
"maxthreads": threads,
}
}
})
deferreds = []
results = []
for i in range(threads):
d = treq.get("http://localhost:8080/", reactor=temp_reactor)
d.addCallback(treq.content)
d.addCallback(results.append)
deferreds.append(d)
def done(_):
max_concurrency = max([int(x) for x in results])
assert max_concurrency == threads, "Maximum concurrency was %s, not %s" % (max_concurrency, threads)
temp_reactor.stop()
defer.DeferredList(deferreds).addCallback(done)
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()