Skip to main content

Go-like features for Python

Project description

Package golang provides Go-like features for Python:

  • go spawns lightweight thread.

  • chan and select provide channels with Go semantic.

  • method allows to define methods separate from class.

  • defer allows to schedule a cleanup from the main control flow.

  • gimport allows to import python modules by full path in a Go workspace.

Goroutines and channels

go spawns a thread, or a coroutine if gevent was activated. It is possible to exchange data in between either threads or coroutines via channels. chan creates a new channel with Go semantic - either synchronous or buffered. Use chan.recv, chan.send and chan.close for communication. select can be used to multiplex on several channels. For example:

ch1 = chan()    # synchronous channel
ch2 = chan(3)   # channel with buffer of size 3

def _():
    ch1.send('a')
    ch2.send('b')
go(_)

ch1.recv()      # will give 'a'
ch2.recv_()     # will give ('b', True)

_, _rx = select(
    ch1.recv,           # 0
    ch2.recv_,          # 1
    (ch2.send, obj2),   # 2
    default,            # 3
)
if _ == 0:
    # _rx is what was received from ch1
    ...
if _ == 1:
    # _rx is (rx, ok) of what was received from ch2
    ...
if _ == 2:
    # we know obj2 was sent to ch2
    ...
if _ == 3:
    # default case
    ...

Methods

method decorator allows to define methods separate from class.

For example:

@method(MyClass)
def my_method(self, ...):
    ...

will define MyClass.my_method().

Defer / recover / panic

defer allows to schedule a cleanup to be executed when current function returns. It is similar to try/finally but does not force the cleanup part to be far away in the end. For example:

wc = wcfs.join(zurl)    │     wc = wcfs.join(zurl)
defer(wc.close)         │     try:
                        │        ...
...                     │        ...
...                     │        ...
...                     │     finally:
                        │        wc.close()

For completeness there is recover and panic that allow to program with Go-style error handling, for example:

def _():
   r = recover()
   if r is not None:
      print("recovered. error was: %s" % (r,))
defer(_)

...

panic("aaa")

But recover and panic are probably of less utility since they can be practically natively modelled with try/except.

If defer is used, the function that uses it must be wrapped with @func or @method decorators.

Import

gimport provides way to import python modules by full path in a Go workspace.

For example

lonet = gimport('lab.nexedi.com/kirr/go123/xnet/lonet')

will import either

  • lab.nexedi.com/kirr/go123/xnet/lonet.py, or

  • lab.nexedi.com/kirr/go123/xnet/lonet/__init__.py

located in src/ under $GOPATH.


Pygolang change history

0.0.0.dev5 (2018-10-30)

  • Fix select bug that was causing several cases to be potentially executed at the same time (commit 1, 2, 3).

  • Add defer and recover (commit). The implementation is partly inspired by work of Denis Kolodin (1, 2).

  • Fix @method on Python3 (commit).

  • A leaked goroutine no longer prevents whole program to exit (commit 1, 2).

0.0.0.dev4 (2018-07-04)

  • Add py.bench program and golang.testing package with corresponding bits (commit).

    py.bench allows to benchmark python code similarly to go test -bench and py.test. For example, running py.bench on the following code:

    def bench_add(b):
        x, y = 1, 2
        for i in xrange(b.N):
            x + y

    gives something like:

    $ py.bench --count=3 x.py
    ...
    pymod: bench_add.py
    Benchmarkadd    50000000        0.020 µs/op
    Benchmarkadd    50000000        0.020 µs/op
    Benchmarkadd    50000000        0.020 µs/op

0.0.0.dev3 (2018-07-02)

  • Support both Python2 and Python3; qq now does not escape printable UTF-8 characters. (commit 1, 2, 3).

  • golang/x/perf/benchlib: New module to load & work with data in Go benchmark format (commit).

0.0.0.dev2 (2018-06-20)

  • Turn into full pygolang: go, chan, select, method and gcompat.qq are provided in addition to gimport (commit). The implementation is not very fast, but should be working correctly including select - select sends for synchronous channels.

0.0.0.dev1 (2018-05-21)

  • Initial release; gimport functionality only (commit).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygolang-0.0.0.dev5.tar.gz (24.8 kB view details)

Uploaded Source

File details

Details for the file pygolang-0.0.0.dev5.tar.gz.

File metadata

  • Download URL: pygolang-0.0.0.dev5.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.7

File hashes

Hashes for pygolang-0.0.0.dev5.tar.gz
Algorithm Hash digest
SHA256 2493346e2c8e39195816220a4327a215cda3b19a1f32e06398737eff616258ce
MD5 766ced114dab1898f17930f2258b4d94
BLAKE2b-256 a945a87fc203e239d8cbb88d62b95c9cad620a78fb2797b0a5fc10abdd8f6829

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page