GithubHelp home page GithubHelp logo

vijayviji / executor Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 1.0 11 KB

Executor is similar to Executors in Java lang. A simple thread pool implemented for golang. This gives you fine control over native threads.

Go 96.38% Makefile 3.62%

executor's Introduction

executor

Executor is similar to Executors in Java language. A simple thread pool implemented for golang. This gives you fine control over native threads.

An executor is backed by thread pool. runtime.LockOSThread() is internally used to make the thread concept available for go programmers. This is very useful, if you want to contain resources for certain type of tasks.

For example, if you want some CPU hunger tasks not starve other simple tasks, you can create two separate thread pools (or executors) and contain the CPU resource to CPU hunger tasks.

Enjoy!

APIs

  • To import

    import "github.com/vijayviji/executor"
  • Create an executor backed by a single thread:

    ex := executor.NewSingleThreadExecutor("executorName", 200)
    // 200 = Task Queue Size
  • Create an executor backed by a pool of threads:

    ex := executor.NewFixedThreadPool("executorName", 10, 2000)
    // 10 = No. of threads
    // 2000 = Task Queue Size of each thread
    // All the tasks queued to this executor will be pushed to task queues of all the threads backing this executor in
    // a round-robin fashion.
  • Queue a task to an executor:

    dataForTask = "Dummy data"
    
    future := ex.Submit(func(taskData interface{}, threadName string, taskID uint64) interface{} {
        dataFromTask := taskData.(string)
        fmt.Println("data for this task: ", dataFromTask)
        return "OKKK"
    }, dataForTask)
    :
    :
    result := future.Get()
    // result will be "OKKK"
    
    // taskStatus can be any of executor.TaskNotStarted, executor.TaskStarted, executor.TaskDone
    taskStatus := future.GetStatus()
  • Shutdown an executor (and all its backing threads):

    ex.Shutdown()
    // This function would only return after all the threads are shut down.
  • Running test on executor.go

    make test

executor's People

Contributors

vijayviji avatar skwiwel avatar

Stargazers

Brian Wang avatar  avatar Warren Guy (Rain) avatar Jonathan Halterman avatar Mertalp Ulus avatar Craig Campbell avatar  avatar yehun avatar Anu Priya avatar  avatar  avatar Alec Petrosky avatar vpromzelev avatar Mike F avatar Dmitry Chusovitin avatar

Watchers

James Cloos avatar  avatar

Forkers

skwiwel

executor's Issues

Prioritizing Goroutines

Hi,

for my work I need goroutines that I can start with a higher priority than others. @vijayviji On Stackoverflow (https://stackoverflow.com/questions/53890318/prioritizing-goroutine-in-golang) you wrote that with this implementation of thread pools in Go (executor) it is possible to prioritize certain goroutines over others. Could you please give a concrete example of this or explain this statement in more detail? Unfortunately, I can't find any parameter for a priority in your code that I could transfer to the executor.

Thank you very much for an answer!

Best regards from Germany

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.