##// END OF EJS Templates
Backport PR #4118: ZMQ heartbeat channel: catch EINTR exceptions and continue....
MinRK -
Show More
@@ -1,56 +1,65
1 1 """The client and server for a basic ping-pong style heartbeat.
2 2 """
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2008-2011 The IPython Development Team
6 6 #
7 7 # Distributed under the terms of the BSD License. The full license is in
8 8 # the file COPYING, distributed as part of this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 import errno
15 16 import os
16 17 import socket
17 18 from threading import Thread
18 19
19 20 import zmq
20 21
21 22 from IPython.utils.localinterfaces import LOCALHOST
22 23
23 24 #-----------------------------------------------------------------------------
24 25 # Code
25 26 #-----------------------------------------------------------------------------
26 27
27 28
28 29 class Heartbeat(Thread):
29 30 "A simple ping-pong style heartbeat that runs in a thread."
30 31
31 32 def __init__(self, context, addr=('tcp', LOCALHOST, 0)):
32 33 Thread.__init__(self)
33 34 self.context = context
34 35 self.transport, self.ip, self.port = addr
35 36 if self.port == 0:
36 37 if addr[0] == 'tcp':
37 38 s = socket.socket()
38 39 # '*' means all interfaces to 0MQ, which is '' to socket.socket
39 40 s.bind(('' if self.ip == '*' else self.ip, 0))
40 41 self.port = s.getsockname()[1]
41 42 s.close()
42 43 elif addr[0] == 'ipc':
43 44 self.port = 1
44 45 while os.path.exists("%s-%s" % (self.ip, self.port)):
45 46 self.port = self.port + 1
46 47 else:
47 48 raise ValueError("Unrecognized zmq transport: %s" % addr[0])
48 49 self.addr = (self.ip, self.port)
49 50 self.daemon = True
50 51
51 52 def run(self):
52 53 self.socket = self.context.socket(zmq.REP)
53 54 c = ':' if self.transport == 'tcp' else '-'
54 55 self.socket.bind('%s://%s' % (self.transport, self.ip) + c + str(self.port))
55 zmq.device(zmq.FORWARDER, self.socket, self.socket)
56
56 while True:
57 try:
58 zmq.device(zmq.FORWARDER, self.socket, self.socket)
59 except zmq.ZMQError as e:
60 if e.errno == errno.EINTR:
61 continue
62 else:
63 raise
64 else:
65 break
General Comments 0
You need to be logged in to leave comments. Login now