Giter Club home page Giter Club logo

parseq's Introduction

ParSeq

ParSeq is a framework that makes it easier to write asynchronous code in Java.

Some of the key benefits of ParSeq include:

Our Wiki includes an introductory example, a User's Guide, javadoc, and more.

See CHANGELOG for list of changes.

Introductory Example

In this example we show how to fetch several pages in parallel and how to combine them once they've all been retrieved.

You can find source code here: IntroductoryExample.

First we can retrieve a single page using an asynchronous HTTP client as follows:

    final Task<Response> google = HttpClient.get("http://www.google.com").task();
    engine.run(google);
    google.await();
    System.out.println("Google Page: " + google.get().getResponseBody());

This will print:

Google Page: <!doctype html><html>...

In this code snippet we don't really get any benefit from ParSeq. Essentially we create a task that can be run asynchronously, but then we block for completion using google.await(). In this case, the code is more complicated than issuing a simple synchronous call. We can improve this by making it asynchronous:

    final Task<String> google = HttpClient.get("http://www.google.com").task()
        .map(response -> response.getResponseBody())
        .andThen(body -> System.out.println("Google Page: " + body));

    engine.run(google);

We used map method to transform Response into the String and andThen method to print out result. Now, let's expand the example so that we can fetch a few more pages in parallel. First, let's create a helper method that creates a task responsible for fetching page body given a URL.

    private Task<String> fetchBody(String url) {
      return HttpClient.get(url).task()
        .map("getBody", response -> response.getResponseBody());
    }

Next, we will compose tasks to run in parallel using Task.par.

    final Task<String> google = fetchBody("http://www.google.com");
    final Task<String> yahoo = fetchBody("http://www.yahoo.com");
    final Task<String> bing = fetchBody("http://www.bing.com");

    final Task<String> plan = Task.par(google, yahoo, bing)
        .map((g, y, b) -> "Google Page: " + g +" \n" +
                          "Yahoo Page: " + y + "\n" +
                          "Bing Page: " + b + "\n")
        .andThen(System.out::println);

    engine.run(plan);

This example is fully asynchronous. The home pages for Google, Yahoo, and Bing are all fetched in parallel while the original thread has returned to the calling code. We used Tasks.par to tell the engine to parallelize these HTTP requests. Once all of the responses have been retrieved they are transfomred into a String that is finally printed out.

We can do various transforms on the data we retrieved. Here's a very simple transform that sums the length of the 3 pages that were fetched:

    final Task<Integer> sumLengths =
        Task.par(google.map("length", s -> s.length()),
                 yahoo.map("length", s -> s.length()),
                 bing.map("length", s -> s.length()))
             .map("sum", (g, y, b) -> g + y + b);

The sumLengths task can be given to an engine for execution and its result value will be set to the sum of the length of the 3 fetched pages.

Notice that we added descriptions to tasks. e.g. map("sum", (g, y, b) -> g + y + b). Using ParSeq's [[trace visualization tools|Tracing]] we can visualize execution of the plan. Waterfall graph shows tasks execution in time (notice how all GET requests are executed in parallel):

sum-lengths-waterfall-example.png

Graphviz diagram best describes relationships between tasks:

sum-lengths-graphviz-example.png

For more in-depth description of ParSeq please visit User's Guide.

For many more examples, please see the parseq-examples contrib project in the source code.

Build Status

Build Status

License

ParSeq is licensed under the terms of the Apache License, Version 2.0.

parseq's People

Contributors

cpettitt-linkedin avatar angxu avatar chikit avatar michaelbitard avatar sweeboonlim avatar franklinyinanding avatar cpettitt avatar mtagle avatar

Watchers

James Cloos avatar 默书 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.