• This repository has been archived on 15/Mar/2021
  • Stars
    star
    331
  • Rank 127,323 (Top 3 %)
  • Language
    Python
  • License
    Other
  • Created over 14 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An async programming framework with a blocking look-alike syntax.

monocle

An async programming framework with a blocking look-alike syntax.

pypi Build Status

‼️ Monocle only supports Python v2 which has reached end of life (EOL) on January 1st, 2020. We don't recommend to use this package anymore. ‼️

Monocle straightens out event-driven code using Python's generators. It aims to be portable between event-driven I/O frameworks, and currently supports Twisted and Tornado.

It's for Python 2.7 only; the syntax it uses isn't supported in older versions of Python. Monocle has not yet been updated for Python 3.

A Simple Example

Here's a simple monocle program that runs two concurrent lightweight processes (called "o-routines") using Tornado's event loop. One is an HTTP server, and the other makes an HTTP request:

    import monocle
    monocle.init("tornado")

    from monocle import Return
    from monocle.stack import eventloop
    from monocle.stack.network import add_service
    from monocle.stack.network.http import HttpClient, HttpHeaders, HttpServer

    @monocle.o
    def hello_http(req):
        content = "Hello, World!"
        headers = HttpHeaders()
        headers['Content-Length'] = len(content)
        headers['Content-Type'] = 'text/plain'
        yield Return(200, headers, content)

    @monocle.o
    def request():
        resp = yield HttpClient.query('http://127.0.0.1:8088/')
        print resp.code, resp.body

    add_service(HttpServer(8088, hello_http))
    monocle.launch(request)
    eventloop.run()

@_o

It's important that code be dapper and well-dressed, so if you prefer, you can don the monocle and use this handy shortcut for @monocle.o:

    from monocle import _o

    @_o
    def request():
        client = HttpClient()
        resp = yield client.request('http://127.0.0.1:8088/')
        print resp.code, resp.body

It's true, this violates Python's convention that underscores indicate variables for internal use. But rules are for breaking. Live a little.

The Big Idea

Event-driven code can be efficient and easy to reason about, but it often splits up procedures in an unpleasant way. Here's an example of a blocking function to read a request from a user, query a database, and return a result:

    def do_cmd(conn):
        cmd = conn.read_until("\n")
        if cmd.type == "get-address":
            user = db.query(cmd.username)
            conn.write(user.address)
        else:
            conn.write("unknown command")

Here's the same thing in event-driven style, using callbacks:

    def get_cmd(conn):
        conn.read_until("\n", callback=handle_cmd)

    def handle_cmd(conn, cmd):
        if cmd.type == "get-address":
            # keep track of the conn so we can write the response back!
            def callback(result):
                handle_user_query_result(conn, result)
            db.query(cmd.username, callback)
        else:
            conn.write("unknown command")

    def handle_user_query_result(conn, user):
        conn.write(user.address)

What started out as a single function in the blocking code has expanded here into four functions (counting the callback closure that captures conn in handle_cmd). In real event-driven code, this kind of thing happens a lot. Any time we want to do I/O, we have to register a new handler and return back out to the event loop to let other things happen while we wait for the I/O to finish. It would be nice if we had some way to tell the event loop to call back into the middle of our function, so we could just continue where we left off.

Fortunately, Python has a mechanism that lets us do exactly that, called generators. Monocle uses generators to straighten out event-driven code.

Here's the monocle equivalent of the event-based code above:

    @_o
    def do_cmd(conn):
        cmd = yield conn.read_until("\n")
        if cmd.type == "get-address":
            user = yield db.query(cmd.username)
            yield conn.write(user.address)
        else:
            yield conn.write("unknown command")

It's event-driven for efficient concurrency, but otherwise looks a lot like the original blocking code. Each time you see the word yield in the code above, the o-routine is returning back up to the event loop and waiting to be called back when the I/O it requested completes.

This approach is a kind of cooperative concurrency that makes for simpler code than callback-based event-driven code, but which we think is easier to reason about than multi-threaded code.

A word about the word yield

In ordinary Python generators, the norm is to think of yield as in crops: the generator yields a value. In monocle o-routines, it's helpful to think of yield as in traffic. yield conn.read(10) in an o-routine means "yield to other o-routines until we finish reading 10 bytes".

Developer information

To run individual tests on your computer install py.test and the packages for the backend you’d like to test. Here’s how to do it in a separate virtualenv with Twisted:

$ cd monocle/
$ virtualenv --clear --no-site-package .venv
$ .venv/bin/pip install pytest twisted
...

You run the tests like this:

$ .venv/bin/python o_test.py twisted tests/

Alternatively you can use tox to run all the tests for the different backends:

$ .venv/bin/pip install tox
...
$ tox

Check tox.ini to see how the tests are run.

Who?

Monocle was created by Greg Hazel and Steven Hazel.

Related Work

monocle is similar to, and takes inspiration from:

  • Twisted's inlineCallbacks
  • BitTorrent's yielddefer (used in the 5.x mainline client)
  • diesel
  • Go's goroutines (and CSP generally)
  • Haskell's I/O monad
  • eventlet

More Repositories

1

the-internet

An example application that captures prominent and ugly functionality found on the web. Perfect for writing automated acceptance tests against.
CSS
712
star
2

network-viewer

React.js component for viewing HAR files.
JavaScript
96
star
3

node-saucelabs

A wrapper around Sauce Labs API
JavaScript
92
star
4

sample-app-mobile

This is the Sauce Labs Native Sample Application which is designed to be used with mobile devices
JavaScript
86
star
5

docusaurus-theme-github-codeblock

A Docusaurus v2 plugin that supports referencing code examples from public GitHub repositories.
TypeScript
81
star
6

foxdriver

Foxdriver is a Node library which provides a high-level API to control Firefox over the Remote Debugging Protocol
JavaScript
65
star
7

sauce-java

A set of helpers for consuming Sauce Labs services from Java
Java
45
star
8

saucerest-java

Sauce REST API client for Java
Java
43
star
9

my-demo-app-rn

TypeScript
38
star
10

tracelib

A Node.js library that provides Chrome DevTools trace models to parse arbitrary trace logs.
TypeScript
35
star
11

saucectl

A command line interface to run testrunner tests
Go
33
star
12

check-my-repo

Automatically check repositories health and quality and build reports that help us understand the current state of Sauce Labs repositories
Vue
31
star
13

sample-app-web

This is the Sauce Labs Sample Application which is designed to be used from desktop web browsers
JavaScript
26
star
14

testrunner-toolkit

Sauce Labs Test Runner Toolkit is a JavaScript native approach to perform browser testing in a CI with Sauce Labs
HTML
23
star
15

sauce_bindings

Making test automation with Sauce Labs insanely simple
Java
23
star
16

forwarder

Fast HTTP(S) proxy with PAC support
Go
21
star
17

sauce_whisk

ActiveRecord style client for the Sauce Labs RESTful API
Ruby
21
star
18

sauce-docs

Documentation for the Sauce Labs Platform
JavaScript
19
star
19

speedo

Simple performance testing tool using SauceLabs
JavaScript
19
star
20

elemental-next

A free, once-weekly e-mail on how to do test automation like a Pro
MDX
18
star
21

saucectl-cypress-example

A showcase of saucectl running cypress.
JavaScript
14
star
22

my-demo-app-android

Java
14
star
23

ci-sauce

A project where shared logic between our Java based CI plugins (Jenkins, Bamboo) is kept
Java
14
star
24

sauce-connect-action

A GitHub action to launch Sauce Connect Proxy.
TypeScript
13
star
25

my-demo-app-ios

Swift
11
star
26

sauce-connect-docker

Sauce Connect Docker App
JavaScript
11
star
27

bamboo_sauce

Bamboo plugin for use with Sauce OnDemand
HTML
11
star
28

saucectl-puppeteer-example

A showcase of saucectl running puppeteer + jest.
JavaScript
10
star
29

visual-examples

TypeScript
9
star
30

saucelabs-vusb-app

This project is an Open Source Virtual USB GUI for Sauce Labs Virtual USB. It provides a simple GUI to start an Android or iOS vUSB session with only a few clicks.
JavaScript
8
star
31

node-saucectl

Node.js wrapper for saucectl: Sauce Labs Testrunner Toolkit
JavaScript
8
star
32

sypl

Simple Yet Powerful Logger
Go
8
star
33

saucectl-run-action

GitHub Action for running saucectl test suites.
JavaScript
8
star
34

saucectl-espresso-example

A showcase of saucectl running espresso.
Shell
7
star
35

saucectl-playwright-example

A showcase of saucectl running playwright.
JavaScript
7
star
36

appium-inspector-saucelabs

JavaScript
7
star
37

sauce-cypress-plugin

Cypress Plugin to report your results directly to Sauce Labs
TypeScript
6
star
38

py-ccloud

Simple Python library wrapping the Confluent Cloud CLI
Python
6
star
39

sauce-puppeteer-runner

Sauce Labs test runner image for saucectl to run Puppeteer tests using Sauce Labs Testrunner Toolkit.
JavaScript
6
star
40

sauce-playwright-runner

Sauce Labs test runner image for saucectl to run Playwright tests using Sauce Labs Testrunner Toolkit.
JavaScript
6
star
41

performance-js-examples

JavaScript
6
star
42

saucelabs.github.io

Website of the Sauce Labs Open Source Program Office
SCSS
5
star
43

sauce-togo

Run tests on your infrastructure and see the results in Sauce Labs.
Java
4
star
44

extended-debugging-demo

Demo to show off extended debugging functionality
JavaScript
4
star
45

saucectl-testcafe-example

A showcase of saucectl running testcafe.
JavaScript
4
star
46

sauce-school

Training modules for Sauce Labs
HTML
4
star
47

sauce-cypress-runner

Sauce Labs test runner image for saucectl to run Cypress tests using Sauce Labs Testrunner Toolkit.
JavaScript
4
star
48

saucectl-xcuitest-example

A showcase of saucectl running XCUITest.
Swift
3
star
49

lumberjack

Lumberjack
Go
3
star
50

sauce-security-action

A GitHub action to run security scans on your applications.
TypeScript
3
star
51

sa11y

Selenium Accessibility Project
Ruby
3
star
52

salsa_verde

A simple Java test library optimized for remote execution
Java
3
star
53

py-confluent

Simple Python library wrapping the Confluent Cloud CLI v2
Python
3
star
54

saucectl-run-orb

CircleCI ORB for running saucectl
Shell
3
star
55

performance-samples

Samples demonstrating Sauce Performance features.
JavaScript
3
star
56

teamcity-sauce-ondemand-plugin

Plugin for TeamCity which provides integration with Sauce Labs
Java
3
star
57

randomness

Provides a secure random integer generator, optionally retryable, and collision-free.
Go
3
star
58

replay-chrome-extension

Sauce chrome extension for puppeteer replay
JavaScript
3
star
59

sauce-testcafe-runner

Sauce Labs test runner image for saucectl to run Testcafe tests using Sauce Labs Testrunner Toolkit.
TypeScript
3
star
60

puppeteer-replay-runner

TypeScript
3
star
61

performance-python-examples

extended-debugging-Python-examples
Python
2
star
62

playwright-test-demo

Example of implementation of @playwright/test on Sauce Labs
JavaScript
2
star
63

saucectl-replay-example

2
star
64

CircleCI-SauceLabs-ORB

Shell
2
star
65

performance-CI-demo

A demo that shows how to integrate Speedo in your CI/CD
JavaScript
2
star
66

charts

Curated Sauce Labs Helm charts.
Mustache
2
star
67

testcafe-reporter-prometheus-multi

Prometheus reporter for TestCafe allowing for alerts based on multiple suite executions across time.
JavaScript
2
star
68

testcafe-reporter

JavaScript
2
star
69

diagnoss

Metrics Tracking for Open Source Software
JavaScript
2
star
70

node-zap

OWASP ZapProxy bindings for Node.js
TypeScript
2
star
71

saucectl-apitest-example

Demonstrate running api tests with saucectl
2
star
72

new-project

Template to use when creating a new open source project. It comes with all the standard files which there is expected to be in an open source project on Github.
2
star
73

sauce-elixir

Elixir client library for Sauce Labs API
Elixir
1
star
74

vso-sauce-ondemand-plugin

JavaScript
1
star
75

backstage-test

1
star
76

customerror

Custom errors standardizes errors across applications
Go
1
star
77

.github

Default community health files for Sauce Labs projects
1
star
78

homebrew-saucectl

Homebrew Formulae to saucectl binaries
Ruby
1
star
79

headless-demo-ruby

Demo ruby/watir specs for Headless testing
Ruby
1
star
80

sauce-runner-utils

TypeScript
1
star
81

sauce-json-reporter-js

TypeScript
1
star
82

simple_sauce_js

1
star
83

pacman

The PAC multi-source retriever, and parser. It supports dynamically injecting credentials for proxies specified in the PAC content.
Go
1
star
84

ios-apps-for-testing

a simple app that makes http connections for an endpoint you want and return the status code and the html content.
Swift
1
star
85

sauce-runners

1
star
86

extended-debugging-Ruby-examples

Ruby
1
star