Mercurial > templog
comparison server/utils.py @ 89:51d889ad39a3
main.c : add a delay before turning on uart
server : try and improve timeouts, get rid of lightblue, don't use https,
use python logging module
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 15 Jul 2012 21:49:59 +0800 |
parents | c5ad12670cae |
children |
comparison
equal
deleted
inserted
replaced
88:6f4497a448e8 | 89:51d889ad39a3 |
---|---|
1 import os | 1 import os |
2 import sys | 2 import sys |
3 #import ctypes | 3 #import ctypes |
4 import time | 4 import time |
5 import select | 5 import select |
6 import logging | |
6 | 7 |
7 lightblue = None | 8 L = logging.info |
8 try: | 9 W = logging.warning |
9 import lightblue | 10 E = logging.error |
10 except ImportError: | |
11 pass | |
12 | 11 |
13 DEFAULT_TRIES = 3 | 12 DEFAULT_TRIES = 3 |
14 READLINE_SELECT_TIMEOUT = 4 | 13 READLINE_SELECT_TIMEOUT = 1 |
15 | 14 |
16 __all__ = ('monotonic_time', 'retry') | 15 __all__ = ('monotonic_time', 'retry') |
17 | 16 |
18 clock_gettime = None | 17 clock_gettime = None |
19 no_clock_gettime = True | 18 no_clock_gettime = True |
32 try: | 31 try: |
33 librt = ctypes.CDLL('librt.so.0', use_errno=True) | 32 librt = ctypes.CDLL('librt.so.0', use_errno=True) |
34 clock_gettime = librt.clock_gettime | 33 clock_gettime = librt.clock_gettime |
35 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] | 34 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] |
36 except: | 35 except: |
37 print>>sys.stderr, "No clock_gettime(), using fake fallback." | 36 W("No clock_gettime(), using fake fallback.") |
38 no_clock_gettime = True | 37 no_clock_gettime = True |
39 return time.time() | 38 return time.time() |
40 | 39 |
41 t = timespec() | 40 t = timespec() |
42 CLOCK_MONOTONIC = 1 # see <linux/time.h> | 41 CLOCK_MONOTONIC = 1 # see <linux/time.h> |
51 def retry(retries=DEFAULT_TRIES, try_time = 1): | 50 def retry(retries=DEFAULT_TRIES, try_time = 1): |
52 def inner(func): | 51 def inner(func): |
53 def new_f(*args, **kwargs): | 52 def new_f(*args, **kwargs): |
54 for i in range(retries): | 53 for i in range(retries): |
55 d = func(*args, **kwargs) | 54 d = func(*args, **kwargs) |
56 if d: | 55 if d is not None: |
57 return d | 56 return d |
58 time.sleep(try_time) | 57 time.sleep(try_time) |
58 return None | |
59 | |
59 new_f.func_name = func.func_name | 60 new_f.func_name = func.func_name |
60 return new_f | 61 return new_f |
61 return inner | 62 return inner |
62 | 63 |
63 def readline(sock): | 64 def readline(sock): |
64 timeout = READLINE_SELECT_TIMEOUT | 65 timeout = READLINE_SELECT_TIMEOUT |
65 buf = '' | 66 buf = '' |
66 while True: | 67 while True: |
67 if not lightblue: | 68 (rlist, wlist, xlist) = select.select([sock], [], [], timeout) |
68 (rlist, wlist, xlist) = select.select([sock], [], [], timeout) | 69 if sock not in rlist: |
69 if sock not in rlist: | 70 # hit timeout |
70 # hit timeout | 71 return None |
71 return None | |
72 | 72 |
73 c = sock.recv(1) | 73 c = sock.recv(1) |
74 if c == '': | 74 if c == '': |
75 # lightblue timeout | 75 # lightblue timeout |
76 return None | 76 return None |
97 uc += 1 | 97 uc += 1 |
98 i += 1 | 98 i += 1 |
99 return crc | 99 return crc |
100 | 100 |
101 def cheap_daemon(): | 101 def cheap_daemon(): |
102 print "Daemonising." | 102 L("Daemonising.") |
103 sys.stdout.flush() | 103 sys.stdout.flush() |
104 sys.stderr.flush() | 104 sys.stderr.flush() |
105 out = file('/dev/null', 'a+') | 105 out = file('/dev/null', 'a+') |
106 os.dup2(out.fileno(), sys.stdout.fileno()) | 106 os.dup2(out.fileno(), sys.stdout.fileno()) |
107 os.dup2(out.fileno(), sys.stderr.fileno()) | 107 os.dup2(out.fileno(), sys.stderr.fileno()) |
109 try: | 109 try: |
110 pid = os.fork() | 110 pid = os.fork() |
111 if pid > 0: | 111 if pid > 0: |
112 sys.exit(0) | 112 sys.exit(0) |
113 except OSError, e: | 113 except OSError, e: |
114 print>>sys.stderr, "Bad fork()" | 114 E("Bad fork()") |
115 sys.exit(1) | 115 sys.exit(1) |
116 | 116 |
117 os.setsid() | 117 os.setsid() |
118 | 118 |
119 try: | 119 try: |
120 pid = os.fork() | 120 pid = os.fork() |
121 if pid > 0: | 121 if pid > 0: |
122 sys.exit(0) | 122 sys.exit(0) |
123 except OSError, e: | 123 except OSError, e: |
124 print>>sys.stderr, "Bad fork()" | 124 E("Bad fork()") |
125 sys.exit(1) | 125 sys.exit(1) |
126 | 126 |
127 | 127 |