Skip to content

Commit 50d3afa

Browse files
committed
statsd and future
1 parent 8b78e1b commit 50d3afa

8 files changed

+227
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
263263

264264
## [code](content/code.md)
265265

266+
## [statsd](content/statsd.md)
267+
268+
## [concurrent](content/concurrent.md)
269+
270+
## [geohash](content/geohash.md)
271+
266272
## [tools](content/tools.md)
267273

268274
## [Other_thing](content/other_thing.md)

code/flask_backdoor.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
import signal
3+
import time
4+
from flask import Flask
5+
from gevent import monkey
6+
from gevent.backdoor import BackdoorServer
7+
8+
9+
monkey.patch_all()
10+
11+
app = Flask(__name__)
12+
def handle_backdoor(num, stack):
13+
server = BackdoorServer(('127.0.0.1', 4998))
14+
server.start()
15+
16+
17+
signal.signal(signal.SIGUSR2, handle_backdoor)
18+
now = time.ctime()
19+
20+
@app.route('/')
21+
def index():
22+
global now
23+
now = time.ctime()
24+
return now
25+
26+
27+
def main():
28+
app.run()
29+
30+
31+
if __name__ == '__main__':
32+
main()

code/future_get.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import time
4+
import requests
5+
import functools
6+
from concurrent import futures
7+
8+
9+
def time_count(func):
10+
@functools.wraps(func)
11+
def wrapper(*args, **kwargs):
12+
start = time.time()
13+
result = func(*args, **kwargs)
14+
end = time.time()
15+
print "time", end - start
16+
return result
17+
return wrapper
18+
19+
20+
urls = ['https://ele.me',
21+
'https://baidu.com',
22+
'https://jd.com',
23+
'https://v2ex.com',
24+
'https://windard.com',
25+
'https://taobao.com',
26+
'https://zhihu.com',
27+
'https://vip.com',
28+
'https://t.tt']
29+
30+
31+
@time_count
32+
def main():
33+
executor = futures.ThreadPoolExecutor()
34+
roads = []
35+
results = []
36+
for url in urls:
37+
future = executor.submit(requests.get, url)
38+
roads.append(future)
39+
40+
for future in futures.as_completed(roads):
41+
result = future.result()
42+
results.append(result.status_code)
43+
44+
executor.shutdown()
45+
return results
46+
47+
48+
@time_count
49+
def sync_main():
50+
with futures.ThreadPoolExecutor() as executor:
51+
roads = executor.map(requests.get, urls)
52+
results = [result.status_code for result in roads]
53+
return results
54+
55+
56+
@time_count
57+
def async_main():
58+
results = []
59+
for url in urls:
60+
results.append(requests.get(url).status_code)
61+
return results
62+
63+
64+
if __name__ == '__main__':
65+
print main()
66+
print async_main()
67+
print sync_main()

code/select_receiver.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
import select
3+
import socket
4+
5+
6+
host = '127.0.0.1'
7+
soa_port = 8760
8+
sob_port = 8761
9+
10+
11+
def handle_client(soa, sob):
12+
fdset = [soa, sob]
13+
while True:
14+
r, w, e = select.select(fdset, [], [])
15+
for sock in r:
16+
data = sock.recv(1024)
17+
print sock.getsockname(), data,
18+
19+
20+
if __name__ == '__main__':
21+
soa = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22+
soa.connect((host, soa_port))
23+
24+
sob = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
25+
sob.connect((host, sob_port))
26+
27+
print dir(soa)
28+
handle_client(soa, sob)

code/select_sender.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
import sys
3+
import time
4+
import random
5+
import socket
6+
7+
host = '127.0.0.1'
8+
port = 8760
9+
10+
11+
def handle_serve(host, port):
12+
13+
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
14+
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
15+
server.bind((host,port))
16+
server.listen(5)
17+
18+
print "Server is running on %s:%s Press Ctrl-C to stop" % (host, port)
19+
20+
while 1:
21+
clientsock,clientaddr = server.accept()
22+
print "Welcome from %s : %s"%(clientaddr[0],clientaddr[1])
23+
while 1:
24+
time.sleep(random.randint(2, 7))
25+
clientsock.sendall(str(time.time())+"\n")
26+
27+
28+
if __name__ == '__main__':
29+
if len(sys.argv) > 1:
30+
port = int(sys.argv[1])
31+
handle_serve(host, port)

code/statsd_demo.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# coding=utf-8
2+
from pystatsd import Client
3+
4+
sc = Client('example.org',8125)
5+
6+
sc.timing('python_test.time',500)
7+
sc.increment('python_test.inc_int')
8+
sc.decrement('python_test.decr_int')
9+
sc.gauge('python_test.gauge', 42)

code/statsd_timing.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
import time
3+
import random
4+
from statsd import StatsClient
5+
import functools
6+
7+
statsd = StatsClient()
8+
9+
10+
@statsd.timer('myfunc')
11+
def myfunc(a, b):
12+
statsd.incr("key.incr")
13+
time.sleep(random.random())
14+
15+
16+
def time_deco(func):
17+
@functools.wraps(func)
18+
def wrap(*args, **kwargs):
19+
start_time = time.time()
20+
func(*args, **kwargs)
21+
statsd.timing('krpc.{}'.format(func.__name__),
22+
1000.0 * (time.time() - start_time))
23+
return wrap
24+
25+
26+
@time_deco
27+
def hello():
28+
time.sleep(random.random())
29+
30+
31+
if __name__ == '__main__':
32+
for i in range(100000):
33+
time.sleep(random.random() / 10)
34+
# myfunc(1, 1)
35+
hello()

code/threading_timer.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
import time
3+
import threading
4+
5+
6+
def on_timer():
7+
print time.time()
8+
set_timer()
9+
10+
11+
def set_timer():
12+
_timer = threading.Timer(10, on_timer)
13+
_timer.start()
14+
15+
16+
set_timer()
17+
while 1:
18+
time.sleep(5)
19+
print 'sleep', time.time()

0 commit comments

Comments
 (0)