本文整理汇总了Python中heartbeat.Heartbeat.start方法的典型用法代码示例。如果您正苦于以下问题:Python Heartbeat.start方法的具体用法?Python Heartbeat.start怎么用?Python Heartbeat.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类heartbeat.Heartbeat
的用法示例。
在下文中一共展示了Heartbeat.start方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_kernel
# 需要导入模块: from heartbeat import Heartbeat [as 别名]
# 或者: from heartbeat.Heartbeat import start [as 别名]
def make_kernel(namespace, kernel_factory,
out_stream_factory=None, display_hook_factory=None):
""" Creates a kernel, redirects stdout/stderr, and installs a display hook
and exception handler.
"""
# If running under pythonw.exe, the interpreter will crash if more than 4KB
# of data is written to stdout or stderr. This is a bug that has been with
# Python for a very long time; see http://bugs.python.org/issue706263.
if sys.executable.endswith('pythonw.exe'):
blackhole = file(os.devnull, 'w')
sys.stdout = sys.stderr = blackhole
sys.__stdout__ = sys.__stderr__ = blackhole
# Install minimal exception handling
sys.excepthook = FormattedTB(mode='Verbose', color_scheme='NoColor',
ostream=sys.__stdout__)
# Create a context, a session, and the kernel sockets.
io.raw_print("Starting the kernel at pid:", os.getpid())
context = zmq.Context()
# Uncomment this to try closing the context.
# atexit.register(context.close)
session = Session(username=u'kernel')
reply_socket = context.socket(zmq.XREP)
xrep_port = bind_port(reply_socket, namespace.ip, namespace.xrep)
io.raw_print("XREP Channel on port", xrep_port)
pub_socket = context.socket(zmq.PUB)
pub_port = bind_port(pub_socket, namespace.ip, namespace.pub)
io.raw_print("PUB Channel on port", pub_port)
req_socket = context.socket(zmq.XREQ)
req_port = bind_port(req_socket, namespace.ip, namespace.req)
io.raw_print("REQ Channel on port", req_port)
hb = Heartbeat(context, (namespace.ip, namespace.hb))
hb.start()
hb_port = hb.port
io.raw_print("Heartbeat REP Channel on port", hb_port)
# Helper to make it easier to connect to an existing kernel, until we have
# single-port connection negotiation fully implemented.
io.raw_print("To connect another client to this kernel, use:")
io.raw_print("-e --xreq {0} --sub {1} --rep {2} --hb {3}".format(
xrep_port, pub_port, req_port, hb_port))
# Redirect input streams and set a display hook.
if out_stream_factory:
sys.stdout = out_stream_factory(session, pub_socket, u'stdout')
sys.stderr = out_stream_factory(session, pub_socket, u'stderr')
if display_hook_factory:
sys.displayhook = display_hook_factory(session, pub_socket)
# Create the kernel.
kernel = kernel_factory(session=session, reply_socket=reply_socket,
pub_socket=pub_socket, req_socket=req_socket)
kernel.record_ports(xrep_port=xrep_port, pub_port=pub_port,
req_port=req_port, hb_port=hb_port)
return kernel
示例2: make_kernel
# 需要导入模块: from heartbeat import Heartbeat [as 别名]
# 或者: from heartbeat.Heartbeat import start [as 别名]
def make_kernel(namespace, kernel_factory,
out_stream_factory=None, display_hook_factory=None):
""" Creates a kernel, redirects stdout/stderr, and installs a display hook
and exception handler.
"""
# Re-direct stdout/stderr, if necessary.
if namespace.no_stdout or namespace.no_stderr:
blackhole = file(os.devnull, 'w')
if namespace.no_stdout:
sys.stdout = sys.__stdout__ = blackhole
if namespace.no_stderr:
sys.stderr = sys.__stderr__ = blackhole
# Install minimal exception handling
sys.excepthook = FormattedTB(mode='Verbose', color_scheme='NoColor',
ostream=sys.__stdout__)
# Create a context, a session, and the kernel sockets.
io.raw_print("Starting the kernel at pid:", os.getpid())
context = zmq.Context()
# Uncomment this to try closing the context.
# atexit.register(context.close)
session = Session(username=u'kernel')
reply_socket = context.socket(zmq.XREP)
xrep_port = bind_port(reply_socket, namespace.ip, namespace.xrep)
io.raw_print("XREP Channel on port", xrep_port)
pub_socket = context.socket(zmq.PUB)
pub_port = bind_port(pub_socket, namespace.ip, namespace.pub)
io.raw_print("PUB Channel on port", pub_port)
req_socket = context.socket(zmq.XREQ)
req_port = bind_port(req_socket, namespace.ip, namespace.req)
io.raw_print("REQ Channel on port", req_port)
hb = Heartbeat(context, (namespace.ip, namespace.hb))
hb.start()
hb_port = hb.port
io.raw_print("Heartbeat REP Channel on port", hb_port)
# Helper to make it easier to connect to an existing kernel, until we have
# single-port connection negotiation fully implemented.
io.raw_print("To connect another client to this kernel, use:")
io.raw_print("-e --xreq {0} --sub {1} --rep {2} --hb {3}".format(
xrep_port, pub_port, req_port, hb_port))
# Redirect input streams and set a display hook.
if out_stream_factory:
sys.stdout = out_stream_factory(session, pub_socket, u'stdout')
sys.stderr = out_stream_factory(session, pub_socket, u'stderr')
if display_hook_factory:
sys.displayhook = display_hook_factory(session, pub_socket)
# Create the kernel.
kernel = kernel_factory(session=session, reply_socket=reply_socket,
pub_socket=pub_socket, req_socket=req_socket)
kernel.record_ports(xrep_port=xrep_port, pub_port=pub_port,
req_port=req_port, hb_port=hb_port)
return kernel
示例3: run
# 需要导入模块: from heartbeat import Heartbeat [as 别名]
# 或者: from heartbeat.Heartbeat import start [as 别名]
def run(func, args):
config.init('../xmpp.conf')
client_jid = config.get('users', 'client_jid')
client_password = config.get('users', 'client_password')
server_jid = config.get('users', 'server_jid')
session = Remote.new_session(client_jid, client_password)
endpoint = session.new_proxy(server_jid + '/rpc', TestRunner)
job = Future()
heartbeat = Heartbeat(job, endpoint.ping, session)
heartbeat.start()
getattr(endpoint.async(job), func)(*args)
示例4: IPKernelApp
# 需要导入模块: from heartbeat import Heartbeat [as 别名]
# 或者: from heartbeat.Heartbeat import start [as 别名]
#.........这里部分代码省略.........
def init_connection_file(self):
if not self.connection_file:
self.connection_file = "kernel-%s.json"%os.getpid()
try:
self.load_connection_file()
except Exception:
self.log.error("Failed to load connection file: %r", self.connection_file, exc_info=True)
self.exit(1)
def init_sockets(self):
# Create a context, a session, and the kernel sockets.
self.log.info("Starting the kernel at pid: %i", os.getpid())
context = zmq.Context.instance()
# Uncomment this to try closing the context.
# atexit.register(context.term)
self.shell_socket = context.socket(zmq.ROUTER)
self.shell_port = self._bind_socket(self.shell_socket, self.shell_port)
self.log.debug("shell ROUTER Channel on port: %i" % self.shell_port)
self.iopub_socket = context.socket(zmq.PUB)
self.iopub_port = self._bind_socket(self.iopub_socket, self.iopub_port)
self.log.debug("iopub PUB Channel on port: %i" % self.iopub_port)
self.stdin_socket = context.socket(zmq.ROUTER)
self.stdin_port = self._bind_socket(self.stdin_socket, self.stdin_port)
self.log.debug("stdin ROUTER Channel on port: %i" % self.stdin_port)
self.control_socket = context.socket(zmq.ROUTER)
self.control_port = self._bind_socket(self.control_socket, self.control_port)
self.log.debug("control ROUTER Channel on port: %i" % self.control_port)
def init_heartbeat(self):
"""start the heart beating"""
# heartbeat doesn't share context, because it mustn't be blocked
# by the GIL, which is accessed by libzmq when freeing zero-copy messages
hb_ctx = zmq.Context()
self.heartbeat = Heartbeat(hb_ctx, (self.transport, self.ip, self.hb_port))
self.hb_port = self.heartbeat.port
self.log.debug("Heartbeat REP Channel on port: %i" % self.hb_port)
self.heartbeat.start()
def log_connection_info(self):
"""display connection info, and store ports"""
basename = os.path.basename(self.connection_file)
if basename == self.connection_file or \
os.path.dirname(self.connection_file) == self.profile_dir.security_dir:
# use shortname
tail = basename
if self.profile != 'default':
tail += " --profile %s" % self.profile
else:
tail = self.connection_file
lines = [
"To connect another client to this kernel, use:",
" --existing %s" % tail,
]
# log connection info
# info-level, so often not shown.
# frontends should use the %connect_info magic
# to see the connection info
for line in lines:
self.log.info(line)
# also raw print to the terminal if no parent_handle (`ipython kernel`)
if not self.parent_handle:
for line in lines:
示例5: SchnitzelFactory
# 需要导入模块: from heartbeat import Heartbeat [as 别名]
# 或者: from heartbeat.Heartbeat import start [as 别名]
class SchnitzelFactory(ServerFactory):
protocol = SchnitzelProtocol
def __init__(self, name):
# Configuration
self.configname = name
try:
self.loadConfig()
except IOError:
self.createConfig()
self.saveConfig()
# World
self.url = ""
self.world = World(self.config["world"])
print "SchnitzelFactory created"
def createConfig(self):
self.config = {
"port": 25565,
"name": "SchnitzelCraft",
"motd": "",
"maxplayers": 128,
"saveinterval": 600, # 10 minutes
"ops": [],
"plugins": [],
"magicwand": True,
"public": True,
"heartbeat_endpoint": "http://www.classicube.net/server/heartbeat",
"noverify": False,
"world": "world.dat",
}
print "Created Configuration from defaults"
def loadConfig(self):
with open(self.configname, "r") as fp:
self.config = json.load(fp)
print 'Loaded Configuration from "%s"' % self.configname
def saveConfig(self):
with open(self.configname, "w") as fp:
json.dump(self.config, fp, indent=4)
print 'Saved Configuration to "%s"' % self.configname
def startFactory(self):
# Runtime vars
self.usedIDs = [] # List of Player IDs used (can be occupied by mobs)
self.protocols = {} # Dictionary of Protocols indexed by ID
self.salt = generate_salt()
self.pingtimer = LoopingCall(self.sendPacket, PacketIDs["Ping"])
self.pingtimer.start(1, False)
self.heart = Heartbeat(self, self.config["heartbeat_endpoint"])
self.heart.start()
def save(self):
self.world.save()
self.sendMessage("World saved")
self.saveConfig()
self.savetimer = LoopingCall(save, self)
self.savetimer.start(self.config["saveinterval"], False)
print "SchnitzelFactory started"
def stopFactory(self):
print "SchnitzelFactory stopping..."
for i in self.protocols.itervalues():
i.transport.loseConnection()
self.world.save()
self.saveConfig()
print "SchnitzelFactory stopped"
def sendPacket(self, *packet):
for i in self.protocols.itervalues():
i.sendPacket(*packet)
def sendPacketSkip(self, skip, *packet):
for i in self.protocols.itervalues():
if i != skip:
i.sendPacket(*packet)
def sendMessage(self, msg, pid=255):
while msg:
msgpart = string_to_notch(msg[:64])
msg = msg[64:]
self.sendPacket(PacketIDs["Message"], pid, msgpart)