Pass the response_queue to the constructor, not start().
Removes an unnecessary Thread base-class override. The python documentation also strongly discourages overriding anything other than run().
This commit is contained in:
parent
d6b2cdd595
commit
c07e956127
|
@ -31,13 +31,13 @@ from version import ELECTRUM_VERSION, PROTOCOL_VERSION
|
||||||
from simple_config import SimpleConfig
|
from simple_config import SimpleConfig
|
||||||
|
|
||||||
|
|
||||||
def Interface(server, config = None):
|
def Interface(server, response_queue, config = None):
|
||||||
"""Interface factory function. The returned interface class handles the connection
|
"""Interface factory function. The returned interface class handles the connection
|
||||||
to a single remote electrum server. The object handles all necessary locking. It's
|
to a single remote electrum server. The object handles all necessary locking. It's
|
||||||
exposed API is:
|
exposed API is:
|
||||||
|
|
||||||
- Inherits everything from threading.Thread.
|
- Inherits everything from threading.Thread.
|
||||||
- Member functions start(), send_request(), stop(), is_connected()
|
- Member functions send_request(), stop(), is_connected()
|
||||||
- Member variable server.
|
- Member variable server.
|
||||||
|
|
||||||
"is_connected()" is currently racy. "server" is constant for the object's lifetime and hence
|
"is_connected()" is currently racy. "server" is constant for the object's lifetime and hence
|
||||||
|
@ -45,13 +45,13 @@ def Interface(server, config = None):
|
||||||
"""
|
"""
|
||||||
host, port, protocol = server.split(':')
|
host, port, protocol = server.split(':')
|
||||||
if protocol in 'st':
|
if protocol in 'st':
|
||||||
return TcpInterface(server, config)
|
return TcpInterface(server, response_queue, config)
|
||||||
else:
|
else:
|
||||||
raise Exception('Unknown protocol: %s'%protocol)
|
raise Exception('Unknown protocol: %s'%protocol)
|
||||||
|
|
||||||
class TcpInterface(threading.Thread):
|
class TcpInterface(threading.Thread):
|
||||||
|
|
||||||
def __init__(self, server, config = None):
|
def __init__(self, server, response_queue, config = None):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self.config = config if config is not None else SimpleConfig()
|
self.config = config if config is not None else SimpleConfig()
|
||||||
|
@ -59,6 +59,7 @@ class TcpInterface(threading.Thread):
|
||||||
self.connected = False
|
self.connected = False
|
||||||
self.debug = False # dump network messages. can be changed at runtime using the console
|
self.debug = False # dump network messages. can be changed at runtime using the console
|
||||||
self.message_id = 0
|
self.message_id = 0
|
||||||
|
self.response_queue = response_queue
|
||||||
self.unanswered_requests = {}
|
self.unanswered_requests = {}
|
||||||
# are we waiting for a pong?
|
# are we waiting for a pong?
|
||||||
self.is_ping = False
|
self.is_ping = False
|
||||||
|
@ -275,10 +276,6 @@ class TcpInterface(threading.Thread):
|
||||||
self.connected = False
|
self.connected = False
|
||||||
self.print_error("stopped")
|
self.print_error("stopped")
|
||||||
|
|
||||||
def start(self, response_queue):
|
|
||||||
self.response_queue = response_queue
|
|
||||||
threading.Thread.start(self)
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.s = self.get_socket()
|
self.s = self.get_socket()
|
||||||
if self.s:
|
if self.s:
|
||||||
|
|
|
@ -271,9 +271,9 @@ class Network(util.DaemonThread):
|
||||||
def start_interface(self, server):
|
def start_interface(self, server):
|
||||||
if server in self.interfaces.keys():
|
if server in self.interfaces.keys():
|
||||||
return
|
return
|
||||||
i = interface.Interface(server, self.config)
|
i = interface.Interface(server, self.queue, self.config)
|
||||||
self.pending_servers.add(server)
|
self.pending_servers.add(server)
|
||||||
i.start(self.queue)
|
i.start()
|
||||||
return i
|
return i
|
||||||
|
|
||||||
def start_random_interface(self):
|
def start_random_interface(self):
|
||||||
|
|
|
@ -25,10 +25,10 @@ def send_request(peers, request):
|
||||||
# start interfaces
|
# start interfaces
|
||||||
q2 = Queue.Queue()
|
q2 = Queue.Queue()
|
||||||
config = SimpleConfig()
|
config = SimpleConfig()
|
||||||
interfaces = map ( lambda server: Interface(server, config), peers )
|
interfaces = map( lambda server: Interface(server, q2, config), peers )
|
||||||
reached_servers = []
|
reached_servers = []
|
||||||
for i in interfaces:
|
for i in interfaces:
|
||||||
i.start(q2)
|
i.start()
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
while peers:
|
while peers:
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue