def __call__(self, environ, start_response): name = environ.get("PATH_INFO", "") + "?" + environ.get("QUERY_STRING", "") active = not name.startswith("/static/") if active: ctx.set_log(self.log_url) ctx.set_profile(True) ctx.log_bmark(name) response = self.app(environ, start_response) if active: ctx.set_profile(False) return response
def __call__(self, environ, start_response): name = environ.get("PATH_INFO", "") + "?" + environ.get( "QUERY_STRING", "") active = not name.startswith("/static/") if active: ctx.set_log(self.log_url) ctx.set_profile(True) ctx.log_bmark(name) response = self.app(environ, start_response) if active: ctx.set_profile(False) return response
lock = c.LockWrapper(threading.Lock(), "Archive Lock") def thread(): c.log_bmark("Server thread spawned") for n in range(0, 4): time.sleep(random.random() * 0.5) c.log_start("Processing Video", bookmark=True) c.log_start("Reading file") time.sleep(1 + random.random() * 2.0) c.log_endok("") with lock: c.log_start("Adding to archive") time.sleep(1 + random.random() * 2.0) c.log_endok("") c.log_endok("") if __name__ == "__main__": c.set_log("file://%s" % sys.argv[0].replace(".py", ".ctxt"), append=False) Thread(target=thread, name="Server-1").start() sleep(0.05) Thread(target=thread, name="Server-2").start() sleep(0.05) Thread(target=thread, name="Server-3").start()
import context.api as ctx # your program as normal goes here if __name__ == "__main__": ctx.set_log("file://example.ctxt") ctx.set_profile(True) main()
#!/usr/bin/env python from time import sleep import context.api as c @c.log("Printing hello") def function_a(): print "hello", sleep(0.5) @c.log("Printing world") def function_b(): print "world" sleep(0.1) @c.log("Running program", bookmark=True) def main(): function_a() function_b() if __name__ == "__main__": c.set_log("file://output.py-decorator.ctxt") main()
#!/usr/bin/env python from threading import Thread from time import sleep import random import sys import time import threading sys.path.append("../context-apis/python/") import context.api as c if __name__ == "__main__": c.set_log("file://%s" % sys.argv[0].replace(".py", ".ctxt"), append=False) c.log_start("Rendering Repeat", bookmark=True) for n in range(0, 1000): c.log_start("Frame") time.sleep(0.001) c.log_endok("") c.log_endok() c.log_start("Rendering Alternating", bookmark=True) for n in range(0, 500): c.log_start("Frame A") time.sleep(0.001) c.log_endok("") c.log_start("Frame B") time.sleep(0.001) c.log_endok("")
#!/usr/bin/python import context.api as c import time c.set_log("file://output.py-profile.ctxt") c.set_profile(True) BOARD_SIZE = 8 class BailOut(Exception): pass def validate(queens): time.sleep(0.01) left = right = col = queens[-1] for r in reversed(queens[:-1]): left, right = left - 1, right + 1 if r in (left, col, right): raise BailOut def add_queen(queens): for i in range(BOARD_SIZE): test_queens = queens + [i] try: validate(test_queens) if len(test_queens) == BOARD_SIZE:
def test_set_log(self): c.set_log("file://test.ctxt") # relative c.set_log("file:///tmp/test.ctxt") # absolute self.assertRaises(NotImplementedError, c.set_log, "tcp://localhost:123") self.assertRaises(NotImplementedError, c.set_log, "udp://localhost:123") c.set_log(None) # turn off logging
def setUp(self): self.buf = StringIO() c.set_log(self.buf)
#!/usr/bin/python import context.api as c import time c.set_log("file://output.py-profile.ctxt") c.set_profile(True) BOARD_SIZE = 8 class BailOut(Exception): pass def validate(queens): time.sleep(0.01) left = right = col = queens[-1] for r in reversed(queens[:-1]): left, right = left - 1, right + 1 if r in (left, col, right): raise BailOut def add_queen(queens): for i in range(BOARD_SIZE): test_queens = queens + [i] try: validate(test_queens) if len(test_queens) == BOARD_SIZE: return test_queens else: return add_queen(test_queens)