Skip to content

Commit

Permalink
Reformat Python examples to placate linter
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed Jun 19, 2017
1 parent 2d9cdb8 commit 7b8b2d4
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 130 deletions.
104 changes: 56 additions & 48 deletions extras/example/app/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import socket
import sys
import requests
import random
import threading
import logging
import argparse
Expand All @@ -18,68 +16,78 @@
sessions = threading.local()
args = None


def do_redis():
redis.incr('hits')
return redis.get('hits')
redis.incr('hits')
return redis.get('hits')


def do_qotd():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.settimeout(args.timeout)
s.connect((args.qotd, 4446))
s.send("Hello")
return s.recv(1024)
finally:
s.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.settimeout(args.timeout)
s.connect((args.qotd, 4446))
s.send("Hello")
return s.recv(1024)
finally:
s.close()


def do_search():
if getattr(sessions, 'session', None) == None:
sessions.session = requests.Session()
r = sessions.session.get(args.search, timeout=args.timeout)
return r.text
if getattr(sessions, 'session', None) is None:
sessions.session = requests.Session()
r = sessions.session.get(args.search, timeout=args.timeout)
return r.text


def do_echo(text):
if getattr(sessions, 'session', None) == None:
sessions.session = requests.Session()
r = sessions.session.get(args.echo, data=text, timeout=args.timeout)
return r.text
if getattr(sessions, 'session', None) is None:
sessions.session = requests.Session()
r = sessions.session.get(args.echo, data=text, timeout=args.timeout)
return r.text


def ignore_error(f):
try:
return str(f())
except:
logging.error("Error executing function", exc_info=sys.exc_info())
return "Error"
try:
return str(f())
except:
logging.error("Error executing function", exc_info=sys.exc_info())
return "Error"


# this root is for the tracing demo
@app.route('/hello')
def hello():
qotd_msg = do_qotd()
qotd_msg = do_echo(qotd_msg)
return qotd_msg
qotd_msg = do_qotd()
qotd_msg = do_echo(qotd_msg)
return qotd_msg


# this is for normal demos
@app.route('/')
def root():
counter_future = pool.submit(do_redis)
search_future = pool.submit(do_search)
qotd_future = pool.submit(do_qotd)
echo_future = pool.submit(lambda: do_echo("foo"))
result = 'Hello World! I have been seen %s times.' % ignore_error(counter_future.result)
result += ignore_error(search_future.result)
result += ignore_error(qotd_future.result)
result += ignore_error(echo_future.result)
return result
counter_future = pool.submit(do_redis)
search_future = pool.submit(do_search)
qotd_future = pool.submit(do_qotd)
echo_future = pool.submit(lambda: do_echo("foo"))
result = 'Hello World! I have been seen %s times.' % ignore_error(
counter_future.result)
result += ignore_error(search_future.result)
result += ignore_error(qotd_future.result)
result += ignore_error(echo_future.result)
return result


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-redis', default="redis.weave.local")
parser.add_argument('-search', default="http://search.weave.local:80/")
parser.add_argument('-qotd', default="qotd.weave.local")
parser.add_argument('-echo', default="http://echo.weave.local:80/")
parser.add_argument('-timeout', default=0.5, type=float)
args = parser.parse_args()

logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO)
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(host="0.0.0.0", port=80, debug=True)
parser = argparse.ArgumentParser()
parser.add_argument('-redis', default="redis.weave.local")
parser.add_argument('-search', default="http://search.weave.local:80/")
parser.add_argument('-qotd', default="qotd.weave.local")
parser.add_argument('-echo', default="http://echo.weave.local:80/")
parser.add_argument('-timeout', default=0.5, type=float)
args = parser.parse_args()

logfmt = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s'
logging.basicConfig(format=logfmt, level=logging.INFO)
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(host="0.0.0.0", port=80, debug=True)
87 changes: 46 additions & 41 deletions extras/example/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,58 @@
import socket
import sys


def do_request(s, args):
addrs = socket.getaddrinfo(args.target, args.port)
addrs = [a
for a in addrs
if a[0] == socket.AF_INET]
if len(addrs) <= 0:
logging.info("Could not resolve %s", args.target)
return
addr = random.choice(addrs)
url = "http://%s:%d%s" % (addr[4][0], args.port, args.path)
s.get(url, timeout=args.timeout)
logging.info("Did request %s", url)
addrs = socket.getaddrinfo(args.target, args.port)
addrs = [a for a in addrs if a[0] == socket.AF_INET]
if len(addrs) <= 0:
logging.info("Could not resolve %s", args.target)
return
addr = random.choice(addrs)
url = "http://%s:%d%s" % (addr[4][0], args.port, args.path)
s.get(url, timeout=args.timeout)
logging.info("Did request %s", url)


def do_requests(args):
s = requests.Session()
while True:
try:
if args.persist:
do_request(s, args)
else:
do_request(requests.Session(), args)
except:
logging.error("Error doing request", exc_info=sys.exc_info())
s = requests.Session()
while True:
try:
if args.persist:
do_request(s, args)
else:
do_request(requests.Session(), args)
except:
logging.error("Error doing request", exc_info=sys.exc_info())

time.sleep(args.period)
time.sleep(args.period)


def main():
parser = argparse.ArgumentParser()
parser.add_argument('-target', default="frontend.weave.local")
parser.add_argument('-port', default=80, type=int)
parser.add_argument('-path', default="/")
parser.add_argument('-concurrency', default=1, type=int)
parser.add_argument('-persist', default=True, type=bool)
parser.add_argument('-timeout', default=1.0, type=float)
parser.add_argument('-period', default=0.1, type=float)
args = parser.parse_args()

logging.info("Starting %d threads, targeting %s", args.concurrency, args.target)
threads = [threading.Thread(target=do_requests, args=(args,))
for i in range(args.concurrency)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
logging.info("Exiting")
parser = argparse.ArgumentParser()
parser.add_argument('-target', default="frontend.weave.local")
parser.add_argument('-port', default=80, type=int)
parser.add_argument('-path', default="/")
parser.add_argument('-concurrency', default=1, type=int)
parser.add_argument('-persist', default=True, type=bool)
parser.add_argument('-timeout', default=1.0, type=float)
parser.add_argument('-period', default=0.1, type=float)
args = parser.parse_args()

logging.info("Starting %d threads, targeting %s", args.concurrency,
args.target)
threads = [
threading.Thread(target=do_requests, args=(args, ))
for i in range(args.concurrency)
]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
logging.info("Exiting")


if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO)
main()
logfmt = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s'
logging.basicConfig(format=logfmt, level=logging.INFO)
main()
17 changes: 7 additions & 10 deletions extras/example/echo/echo.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import os
import socket
import sys
import random
import time
import threading
import logging

from flask import Flask
Expand All @@ -12,11 +6,14 @@

app = Flask(__name__)


@app.route('/')
def echo():
return request.data
return request.data


if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO)
WSGIRequestHandler.protocol_version = "HTTP/1.0"
app.run(host="0.0.0.0", port=80, debug=True)
logfmt = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s'
logging.basicConfig(format=logfmt, level=logging.INFO)
WSGIRequestHandler.protocol_version = "HTTP/1.0"
app.run(host="0.0.0.0", port=80, debug=True)
70 changes: 39 additions & 31 deletions extras/example/trace_app/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import socket
import sys
import requests
Expand All @@ -18,52 +17,61 @@

searchapps = ['http://searchapp:8080/']


def do_redis():
redis.incr('hits')
return redis.get('hits')
redis.incr('hits')
return redis.get('hits')


def do_qotd():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(("qotd.weave.local", 4446))
s.send("Hello")
return s.recv(1024)
finally:
s.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(("qotd.weave.local", 4446))
s.send("Hello")
return s.recv(1024)
finally:
s.close()


def do_search():
if getattr(sessions, 'session', None) == None:
sessions.session = requests.Session()
r = sessions.session.get(random.choice(searchapps))
return r.text
if getattr(sessions, 'session', None) is None:
sessions.session = requests.Session()
r = sessions.session.get(random.choice(searchapps))
return r.text


def do_echo(text):
r = requests.get("http://echo.weave.local/", data=text)
return r.text
r = requests.get("http://echo.weave.local/", data=text)
return r.text


def ignore_error(f):
try:
return str(f())
except:
logging.error("Error executing function", exc_info=sys.exc_info())
return "Error"
try:
return str(f())
except:
logging.error("Error executing function", exc_info=sys.exc_info())
return "Error"


# this root is for the tracing demo
@app.route('/hello')
def hello():
qotd_msg = do_qotd()
qotd_msg = do_echo(qotd_msg)
return qotd_msg
qotd_msg = do_qotd()
qotd_msg = do_echo(qotd_msg)
return qotd_msg


# this is for normal demos
@app.route('/')
def root():
#counter_future = pool.submit(do_redis)
#search_future = pool.submit(do_search)
result = do_echo(do_qotd())
return result
# counter_future = pool.submit(do_redis)
# search_future = pool.submit(do_search)
result = do_echo(do_qotd())
return result


if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO)
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(host="0.0.0.0", port=80, debug=True)
logfmt = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s'
logging.basicConfig(format=logfmt, level=logging.INFO)
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(host="0.0.0.0", port=80, debug=True)

0 comments on commit 7b8b2d4

Please sign in to comment.