python

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const SerializerMetaKey = "_python_serializer"

Variables

This section is empty.

Functions

func DropGlobalReferences added in v1.5.0

func DropGlobalReferences(objs []py.PyObjectPtr, ctx context.Context) error

DropGlobalReferences to a list of PyObjectPtr.

func Evaluate added in v1.7.0

func Evaluate(fn func() error, c chan error, ctx context.Context) error

Evaluate a given function fn in the context of the main interpreter. A response is provided via the channel c.

XXX This may look a little odd, both returning an error and using a channel, but it's to avoid having to allocate a channel for each call as this function will be called frequently.

func Spawn added in v1.7.0

func Spawn(legacyMode bool, ctx context.Context) (*subInterpreter, error)

Spawn a new sub-interpreter.

func StopSub added in v1.7.0

func StopSub(subInterpreter *subInterpreter, ctx context.Context) error

StopSub will attempt to shut down a sub-interpreter.

Types

type Callback added in v1.4.0

type Callback struct {
	Name       string
	Definition *py.PyMethodDef
	Object     py.PyObjectPtr
}

func NewCallback added in v1.4.0

func NewCallback(name string, f callbackFunc) (*Callback, error)

type ContextAwareMutex

type ContextAwareMutex struct {
	// contains filtered or unexported fields
}

A ContextAwareMutex implements a mutex lock that allows for an optional context to be provided so a caller can control if they want a deadline or timeout.

func NewContextAwareMutex

func NewContextAwareMutex() *ContextAwareMutex

func (*ContextAwareMutex) AssertLocked

func (c *ContextAwareMutex) AssertLocked()

AssertLocked panics if the ContextAwareMutex is not currently locked by someone.

func (*ContextAwareMutex) Lock

func (c *ContextAwareMutex) Lock() error

Lock the mutex using a background context.

func (*ContextAwareMutex) LockWithContext

func (c *ContextAwareMutex) LockWithContext(ctx context.Context) error

LockWithContext will attempt to take the lock but uses the provided context.Context to allow cancellation.

func (*ContextAwareMutex) Unlock

func (c *ContextAwareMutex) Unlock()

Unlock the mutex. Panics if the mutex is not currently locked in order to speed up debugging.

type InterpreterTicket

type InterpreterTicket struct {
	// contains filtered or unexported fields
}

An InterpreterTicket represents ownership of the interpreter of a particular Runtime. Other than its id, it's opaque to the user.

func (*InterpreterTicket) Id

func (i *InterpreterTicket) Id() int64

Id provides a unique (to the backing Runtime) identifier for an interpreter. The caller may use this for identifying if they're re-using an interpreter.

type Mode added in v1.4.0

type Mode string
const (
	Isolated       Mode = "isolated"
	Global         Mode = "global"
	IsolatedLegacy Mode = "isolated_legacy"
	InvalidMode    Mode = "invalid"
)

func StringAsMode added in v1.4.0

func StringAsMode(s string) Mode

type MultiInterpreterRuntime

type MultiInterpreterRuntime struct {
	// contains filtered or unexported fields
}

MultiInterpreterRuntime creates and manages multiple Python sub-interpreters.

func NewMultiInterpreterRuntime

func NewMultiInterpreterRuntime(exe string, cnt int, legacyMode bool, logger *service.Logger) (*MultiInterpreterRuntime, error)

func (*MultiInterpreterRuntime) Acquire

func (*MultiInterpreterRuntime) Apply

func (r *MultiInterpreterRuntime) Apply(ticket *InterpreterTicket, _ context.Context, f func() error) error

func (*MultiInterpreterRuntime) Map

Map a function fn over all the interpreters, one at a time. Useful for initializing all interpreters to a given state.

func (*MultiInterpreterRuntime) Release

func (r *MultiInterpreterRuntime) Release(ticket *InterpreterTicket) error

func (*MultiInterpreterRuntime) Start

Start the Python runtime. A MultiInterpreterRuntime centralizes modification of the main interpreter in a go routine.

func (*MultiInterpreterRuntime) Stop

Stop a running Python Runtime.

type Runtime

type Runtime interface {
	// Start the Python runtime.
	Start(ctx context.Context) error

	// Stop the Python runtime, removing all interpreter state.
	Stop(ctx context.Context) error

	// Acquire ownership of an interpreter until Release is called, providing
	// a ticket on success or returning err on error.
	Acquire(ctx context.Context) (ticket *InterpreterTicket, err error)

	// Release ownership of an interpreter identified by the given
	// InterpreterTicket.
	Release(token *InterpreterTicket) error

	// Apply a function f over the interpreter described by the given
	// InterpreterTicket.
	Apply(token *InterpreterTicket, ctx context.Context, f func() error) error

	// Map a function f over all possible interpreters.
	// In the case of multiple interpreters, an error aborts mapping over the
	// remainder.
	Map(ctx context.Context, f func(ticket *InterpreterTicket) error) error
}

A Runtime for a Python interpreter.

type Serializer added in v1.4.0

type Serializer struct {
	// contains filtered or unexported fields
}

func NewSerializer added in v1.4.0

func NewSerializer() (*Serializer, error)

NewSerializer attempts to compile, import, and prepare a set of Python objects to assist serialization.

The caller must manage the interpreter state for this to succeed.

func (*Serializer) DecRef added in v1.4.0

func (s *Serializer) DecRef()

func (*Serializer) JsonBytes added in v1.4.0

func (s *Serializer) JsonBytes(obj py.PyObjectPtr) ([]byte, error)

JsonBytes serializes the given Python object to JSON, encoded to utf-8 bytes.

func (*Serializer) JsonString added in v1.4.0

func (s *Serializer) JsonString(obj py.PyObjectPtr) (string, error)

JsonString serializes the given Python object to JSON.

func (*Serializer) Pickle added in v1.4.0

func (s *Serializer) Pickle(obj py.PyObjectPtr) ([]byte, error)

Pickle the given Python object.

type SerializerMode added in v1.5.0

type SerializerMode string
const (
	// Pickle SerializerMode will pickle any Python results.
	Pickle SerializerMode = "pickle"

	// Bloblang SerializerMode will approximate JSON serialization used by Bloblang.
	Bloblang SerializerMode = "bloblang"

	// None SerializerMode will not attempt serialization and simply pass Python object pointers.
	None SerializerMode = "none"

	InvalidSerializer SerializerMode = "invalid"
)

func StringAsSerializerMode added in v1.5.0

func StringAsSerializerMode(s string) SerializerMode

type SingleInterpreterRuntime

type SingleInterpreterRuntime struct {
	// contains filtered or unexported fields
}

SingleInterpreterRuntime provides an implementation for using main Python interpreter.

func NewSingleInterpreterRuntime

func NewSingleInterpreterRuntime(exe string, cnt int, logger *service.Logger) (*SingleInterpreterRuntime, error)

func (*SingleInterpreterRuntime) Acquire

func (*SingleInterpreterRuntime) Apply

func (r *SingleInterpreterRuntime) Apply(ticket *InterpreterTicket, ctx context.Context, f func() error) error

func (*SingleInterpreterRuntime) Map

func (*SingleInterpreterRuntime) Release

func (r *SingleInterpreterRuntime) Release(ticket *InterpreterTicket) error

func (*SingleInterpreterRuntime) Start

func (*SingleInterpreterRuntime) Stop

Jump to

Keyboard shortcuts

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