commit 489413d358209ea85d28af5e1261a2a3782d289b
parent e6bbf22003a9cf458c20551ee06a908d22d2a66d
Author: ThomasV <thomasv@gitorious>
Date: Tue, 11 Mar 2014 08:33:58 +0100
move start_daemon code to main script
Diffstat:
3 files changed, 31 insertions(+), 37 deletions(-)
diff --git a/electrum b/electrum
@@ -108,33 +108,49 @@ def print_help_cb(self, opt, value, parser):
def run_command(cmd, password=None, args=[]):
- import xmlrpclib, socket
- cmd_runner = Commands(wallet, network)
- func = getattr(cmd_runner, cmd.name)
- cmd_runner.password = password
-
+ import socket
if cmd.requires_network and not options.offline:
-
+ daemon_started = False
while True:
try:
- cmd_runner.network = NetworkProxy(config)
- cmd_runner.network.start()
+ network = NetworkProxy(config)
break
except socket.error:
- if cmd.name != 'daemon':
- print "Starting daemon [%s]"%config.get('server')
- start_daemon(config)
+ if cmd != 'daemon':
+ if not daemon_started:
+ print "Starting daemon [%s]"%config.get('server')
+ daemon_started = True
+ pid = os.fork()
+ if (pid == 0): # The first child.
+ os.chdir("/")
+ os.setsid()
+ os.umask(0)
+ pid2 = os.fork()
+ if (pid2 == 0): # Second child
+ server = NetworkServer(config)
+ try:
+ server.main_loop()
+ except KeyboardInterrupt:
+ print "Ctrl C - Stopping server"
+ sys.exit(1)
+ sys.exit(0)
+ else:
+ time.sleep(0.1)
else:
print "Daemon not running"
sys.exit(1)
+ network = network
+ network.start()
if wallet:
- wallet.start_threads(cmd_runner.network)
+ wallet.start_threads(network)
wallet.update()
else:
- cmd_runner.network = None
-
+ network = None
+ cmd_runner = Commands(wallet, network)
+ func = getattr(cmd_runner, cmd.name)
+ cmd_runner.password = password
try:
result = func(*args[1:])
except Exception:
diff --git a/lib/__init__.py b/lib/__init__.py
@@ -14,4 +14,4 @@ from plugins import BasePlugin
from mnemonic import mn_encode as mnemonic_encode
from mnemonic import mn_decode as mnemonic_decode
from commands import Commands, known_commands
-from daemon import start_daemon, NetworkProxy
+from daemon import NetworkProxy, NetworkServer
diff --git a/lib/daemon.py b/lib/daemon.py
@@ -295,28 +295,6 @@ class NetworkServer:
-def start_daemon(config):
- pid = os.fork()
- if (pid == 0): # The first child.
- os.chdir("/")
- os.setsid()
- os.umask(0)
- pid2 = os.fork()
- if (pid2 == 0): # Second child
- server = NetworkServer(config)
- try:
- server.main_loop()
- except KeyboardInterrupt:
- print "Ctrl C - Stopping server"
- sys.exit(1)
-
- sys.exit(0)
-
- # should use a signal
- time.sleep(2)
-
-
-
if __name__ == '__main__':
import simple_config
config = simple_config.SimpleConfig({'verbose':True, 'server':'ecdsa.net:50002:s'})