commit f7280e46374c442570a2f6b8a97f19a4a1b8944c
parent 44072a4f48683191ed661ca7255e76bbc8300d3c
Author: ThomasV <thomasv@gitorious>
Date: Tue, 29 Jul 2014 10:43:04 +0200
cleanup daemon threads
Diffstat:
3 files changed, 62 insertions(+), 52 deletions(-)
diff --git a/lib/daemon.py b/lib/daemon.py
@@ -59,6 +59,7 @@ def get_daemon(config, start_daemon=True):
time.sleep(0.1)
+
class ClientThread(threading.Thread):
def __init__(self, server, s):
@@ -104,14 +105,15 @@ class ClientThread(threading.Thread):
-class NetworkServer:
+class NetworkServer(threading.Thread):
def __init__(self, config):
+ threading.Thread.__init__(self)
+ self.daemon = True
self.config = config
self.network = Network(config)
# network sends responses on that queue
self.network_queue = Queue.Queue()
- self.network.start(self.network_queue)
self.running = False
# daemon terminates after period of inactivity
@@ -127,10 +129,13 @@ class NetworkServer:
return self.running
def stop(self):
- self.network.stop()
with self.lock:
self.running = False
+ def start(self):
+ self.running = True
+ threading.Thread.start(self)
+
def add_client(self, client):
for key in ['status','banner','updated','servers','interfaces']:
value = self.network.get_status_value(key)
@@ -138,17 +143,13 @@ class NetworkServer:
with self.lock:
self.clients.append(client)
-
def remove_client(self, client):
with self.lock:
self.clients.remove(client)
print_error("client quit:", len(self.clients))
-
-
- def main_loop(self):
- self.running = True
- threading.Thread(target=self.listen_thread).start()
+ def run(self):
+ self.network.start(self.network_queue)
while self.is_running():
try:
response = self.network_queue.get(timeout=0.1)
@@ -157,32 +158,35 @@ class NetworkServer:
for client in self.clients:
client.daemon_pipe.get_queue.put(response)
- print_error("Daemon exiting")
+ self.network.stop()
+ print_error("server exiting")
+
+
- def listen_thread(self):
- self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- self.daemon_port = self.config.get('daemon_port', DAEMON_PORT)
- self.socket.bind(('', self.daemon_port))
- self.socket.listen(5)
- self.socket.settimeout(1)
+def daemon_loop(server):
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ daemon_port = server.config.get('daemon_port', DAEMON_PORT)
+ s.bind(('', daemon_port))
+ s.listen(5)
+ s.settimeout(1)
+ t = time.time()
+ while server.running:
+ try:
+ connection, address = s.accept()
+ except socket.timeout:
+ if not server.clients:
+ if time.time() - t > server.timeout:
+ print_error("Daemon timeout")
+ break
+ else:
+ t = time.time()
+ continue
t = time.time()
- while self.running:
- try:
- connection, address = self.socket.accept()
- except socket.timeout:
- if not self.clients:
- if time.time() - t > self.timeout:
- print_error("Daemon timeout")
- break
- else:
- t = time.time()
- continue
- t = time.time()
- client = ClientThread(self, connection)
- client.start()
- self.stop()
- print_error("listen thread exiting")
+ client = ClientThread(server, connection)
+ client.start()
+ server.stop()
+ print_error("Daemon exiting")
if __name__ == '__main__':
@@ -190,8 +194,10 @@ if __name__ == '__main__':
config = simple_config.SimpleConfig()
util.set_verbosity(True)
server = NetworkServer(config)
+ server.start()
try:
- server.main_loop()
+ daemon_loop(server)
except KeyboardInterrupt:
print "Ctrl C - Stopping server"
+ server.stop()
sys.exit(1)
diff --git a/scripts/block_headers b/scripts/block_headers
@@ -2,10 +2,10 @@
# A simple script that connects to a server and displays block headers
-import time, electrum
+import time
+import electrum
# start network
-
c = electrum.SimpleConfig()
s = electrum.daemon.get_daemon(c,True)
network = electrum.NetworkProxy(s,c)
@@ -20,14 +20,10 @@ if not network.is_connected():
sys.exit(1)
# 2. send the subscription
-callback = lambda result: electrum.print_json(result.get('result'))
+callback = lambda response: electrum.print_json(response.get('result'))
network.send([('blockchain.headers.subscribe',[])], callback)
# 3. wait for results
while network.is_connected():
- try:
- time.sleep(1)
- except:
- break
+ time.sleep(1)
-network.stop()
diff --git a/scripts/watch_address b/scripts/watch_address
@@ -1,6 +1,8 @@
#!/usr/bin/env python
-import sys, time, electrum
+import sys
+import time
+import electrum
try:
addr = sys.argv[1]
@@ -8,19 +10,25 @@ except Exception:
print "usage: watch_address <bitcoin_address>"
sys.exit(1)
+# start network
+c = electrum.SimpleConfig()
+s = electrum.daemon.get_daemon(c,True)
+network = electrum.NetworkProxy(s,c)
+network.start()
-# 1. start the interface and wait for connection
-interface = electrum.Interface('electrum.no-ip.org:50002:s')
-interface.start(wait = True)
-if not interface.is_connected:
- print "not connected"
- exit()
+# wait until connected
+while network.is_connecting():
+ time.sleep(0.1)
+
+if not network.is_connected():
+ print_msg("daemon is not connected")
+ sys.exit(1)
# 2. send the subscription
-callback = lambda _,result: electrum.print_json(result.get('result'))
-interface.send([('blockchain.address.subscribe',[addr])], callback)
+callback = lambda response: electrum.print_json(response.get('result'))
+network.send([('blockchain.address.subscribe',[addr])], callback)
# 3. wait for results
-while True:
+while network.is_connected():
time.sleep(1)