pytasks

package module
v0.0.0-...-5f0c8f7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2019 License: Apache-2.0 Imports: 5 Imported by: 1

README

pytasks

GoDoc CircleCI

Python task execution in Go.

This project embeds Python via CGO and aims to make the Python GIL handling more straight forward.

Installation

Add the package to your go.mod file:

require github.com/nickpoorman/pytasks master

Or, clone the repository:

git clone --branch master https://github.com/nickpoorman/pytasks.git $GOPATH/src/github.com/nickpoorman/pytasks

A complete example:

mkdir pytasks-app && cd pytasks-app

cat > go.mod <<-END
  module my-dataframe-app

  require (
      github.com/nickpoorman/pytasks master
      github.com/DataDog/go-python3 master
  )
END

cat > main.go <<-END
    package main

    import (
        "github.com/DataDog/go-python3"
        "github.com/nickpoorman/pytasks"
    )

    func main() {
        py := pytasks.GetPythonSingleton()
        fooModule, err := py.ImportModule("foo")
        if err != nil {
            panic(err)
        }

        // Start task 1 - this will spawn a new goroutine
        wg1, err := py.NewTask(func() {
            odds := fooModule.GetAttrString("print_odds")
            defer odds.DecRef()

            odds.Call(python3.PyTuple_New(0), python3.PyDict_New())
        })
        if err != nil {
            panic(err)
        }

        // Start task 2 - this will spawn a new goroutine
        wg2, err := py.NewTask(func() {
            even := fooModule.GetAttrString("print_even")
            defer even.DecRef()

            even.Call(python3.PyTuple_New(0), python3.PyDict_New())
        })
        if err != nil {
            panic(err)
        }

        wg1.Wait()
        wg2.Wait()

        // At this point we know we won't need Python anymore in this
        // program, we can restore the state and lock the GIL to perform
        // the final operations before exiting.
        py.Finalize()
    }
END

go run main.go

Usage

See the task_example.go or tests and benchamrks for usage examples.

License

(c) 2019 Nick Poorman. Licensed under the Apache License, Version 2.0.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PythonSingleton

type PythonSingleton interface {
	ImportModule(name string) (*python3.PyObject, error)
	NewTask(task func()) (*sync.WaitGroup, error)
	NewTaskSync(task func()) error
	Finalize() error
}

PythonSingleton is an interface to the pythonSingleton instance

func GetPythonSingleton

func GetPythonSingleton(opts ...PythonSingletonOption) PythonSingleton

GetPythonSingleton returns the existing pythonSingleton or creates a new one if one has not been created yet.

type PythonSingletonOption

type PythonSingletonOption func(ps *pythonSingleton) error

func WithModules

func WithModules(modules []string) PythonSingletonOption

type Tuple

type Tuple struct {
	Result interface{}
	Err    error
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL