annotate server/utils.py @ 328:46070aaf29ea

A bit of work on the server python
author Matt Johnston <matt@ucc.asn.au>
date Sat, 26 May 2012 10:17:27 +0800
parents
children b5925cb4f264
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import sys
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import ctypes
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import time
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import select
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 DEFAULT_TRIES = 3
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 __all__ = ('monotonic_time', 'retry')
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 clock_gettime = None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 no_clock_gettime = False
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 def monotonic_time():
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 global clock_gettime
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 global no_clock_gettime
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 if no_clock_gettime:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 return time.time()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 class timespec(ctypes.Structure):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 _fields_ = [
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 ('tv_sec', ctypes.c_long),
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 ('tv_nsec', ctypes.c_long)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 ]
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 if not clock_gettime:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 try:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 librt = ctypes.CDLL('librt.so.0', use_errno=True)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 clock_gettime = librt.clock_gettime
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 except:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 print>>sys.stderr, "No clock_gettime(), using fake fallback."
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 no_clock_gettime = True
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return time.time()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 t = timespec()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 CLOCK_MONOTONIC = 1 # see <linux/time.h>
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 errno_ = ctypes.get_errno()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 raise OSError(errno_, os.strerror(errno_))
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 return t.tv_sec + t.tv_nsec * 1e-9
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 # decorator, tries a number of times, returns None on failure, sleeps between
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 # Must be used as "@retry()" if arguments are defaulted
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 def retry(retries=DEFAULT_TRIES, try_time = 1):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 def inner(func):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 print "inner"
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 def new_f(*args, **kwargs):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 print "newf"
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 for i in range(retries):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 print "retry %d" % i
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 d = func(*args, **kwargs)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 if d:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 return d
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 time.sleep(try_time)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 new_f.func_name = func.func_name
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 return new_f
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 return inner
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 def readline(sock):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 timeout = READLINE_SELECT_TIMEOUT
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 buf = ''
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 while true:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 (rlist, wlist, xlist) = select.select([sock], [], [], timeout)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if sock not in rlist:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 # hit timeout
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 return None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 c = sock.recv(1)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 if c == '\r':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 continue
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 buf.append(c)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 if c == '\n':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 return buf
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75