News for hackers. Stuff that matters.
Re:Grumpy: Go running Python by sfifs on Wednesday January 04, 2017 @12:41PM (#13320300)
The main reason I moved from coding in Python to Go as my main language many years back is because concurrency was such a pain in standard Python (the other was compile time error checking).

It's interesting to see the same pain has now made caused the runtime itself to be implemented in Go.

It's a pity C extensions (often used in scientific computing) are not supported but Go does have support via CGO, so maybe some approach can be worked out to access C routines in the future.

Re:Grumpy: Go running Python by lelandbatey on Wednesday January 04, 2017 @01:27PM (#13320741)
I "grew up" on Python, then wrote a whole bunch of Go for my job. Then this past Autumn re-visited Python to implement a networked terminal based game[1].

With what I learned about Go and concurrency, I would say that currently in Python, writing concurrent code is not very hard, and is as close to Go as you can get without actually just writing Go.

Now, you may be saying "but Python has the GIL, how can concurrency be easy in Python?" I'd say, you're definitely not wrong that the GIL is a problem, but it's not much of a problem for concurrency.

This goes back to the heart of Rob Pike's classic talk, "Concurrency Is Not Parallelism"[2]. To quote Wikipedia:

        In computer science, concurrency is the decomposability property of a
        program, algorithm, or problem into order-independent or partially-ordered
        components or units.
In Python, you can pretty easily emulate the conceptual properties of Goroutines and Go channels with Python threads and queues. The problem is that doing this in Python won't net you the performance increases you get with Go. And I believe that is an important distinction. There are plenty of cases where you don't care so much about the performance benefits of parallelism, but you want the conceptual and implementation benefits of concurrency.

In closing, concurrency in Python is pretty easy to work with, it just performs very poorly.

[1] - https://github.com/lelandbatey/defuse_division

[2] - https://blog.golang.org/concurrency-is-not-parallelism

Re:Grumpy: Go running Python by rcarmo on Wednesday January 04, 2017 @02:32PM (#13321311)
I wrote a little task module that does precisely that and provides you with a go() function and "channels":

https://github.com/rcarmo/python-utils/blob/master/taskkit.p...

Obviously, it wasn't amazingly performant. But it did help a lot for doing concurrent stuff, and I've been pondering re-doing it for asyncio.

Re:Grumpy: Go running Python by Spiritus on Wednesday January 04, 2017 @02:20PM (#13321197)
Using Gevent[1] is quite similar to Go when it comes to concurrency in Python.

[1] http://sdiehl.github.io/gevent-tutorial/

Re:Grumpy: Go running Python by nhumrich on Thursday January 05, 2017 @09:48AM (#13327456)
Concurrency is even easier with the new async/await syntax
Re:Grumpy: Go running Python by trotterdylan on Wednesday January 04, 2017 @01:01PM (#13320487)
I'm not philosophically opposed to supporting C extensions. The additional complexity just was not deemed to be warranted since the YouTube frontend doesn't use a lot of C extensions.

In principle it's possible to implement something like JyNI (http://jyni.org/) or CPyExt (https://morepypy.blogspot.de/2010/04/using-cpython-extension...) to bridge the CPython and Grumpy APIs. In practice, marshalling data across the interface can be very expensive.

Re:Grumpy: Go running Python by scrollaway on Wednesday January 04, 2017 @01:03PM (#13320514)
Out of curiosity, what C extensions does YouTube use?

If this is good enough to run YouTube's python code already it's honestly super impressive. Well done.

Re:Grumpy: Go running Python by trotterdylan on Wednesday January 04, 2017 @01:20PM (#13320661)
To be clear, Grumpy cannot yet run YouTube's Python codebase. There's still a lot of work to do on the standard library.

There are a handful of C extensions for JSON, protobufs, etc that YouTube uses, but mostly they're small utility functions written by us to optimize particularly hot code paths.

Re:Grumpy: Go running Python by driusan on Wednesday January 04, 2017 @01:51PM (#13320988)
Is the plan to use grumpc to transpile the code to Go and then work in Go in the future, or is the plan to keep coding in Python and add a grumpy step before deploying?

(If the former, you could just update the code to use the standard Go JSON/etc packages..)

Re:Grumpy: Go running Python by trotterdylan on Wednesday January 04, 2017 @02:56PM (#13321508)
The idea is to continue to write code in Python. The transpiled code is not suitable for working with directly. That said, there is the possibility of rewriting bits and pieces in Go (e.g. performance critical stuff) and then call into it from Python. Sort of a hybrid approach.
Re:Grumpy: Go running Python by statsmatscats on Wednesday January 04, 2017 @02:47PM (#13321427)
I"m also curious about this
Re:Grumpy: Go running Python by tym0 on Thursday January 05, 2017 @07:47AM (#13326676)
Will these be rewritten in go then?
Re:Grumpy: Go running Python by vram22 on Wednesday January 04, 2017 @01:00PM (#13320480)
Does anyone know the technical reasons why C extensions are not (at least easily, apparently) supported by Go? Is it to do with Go's being a GC'd language? I would have thought that should not be a reason per se, since Python also has GC, but has plenty of extensions written in C. But I'm not a language internals expert.

Also, further signs that GC may not be the reason, is that D also has GC, but can link to C libraries somewhat easily (not sure about all cases or how far the ease goes).

Re:Grumpy: Go running Python by moosingin3space on Wednesday January 04, 2017 @01:22PM (#13320695)
It's because Go uses a different stack structure, called "segmented stacks", in order to enable cheap goroutines. Basically, Go stacks start tiny (8 KiB, as opposed to much larger C stacks), then it grows them in small segments. Additionally, Go code runs inside an event loop, which enables excellent I/O performance without kernel context-switches, and ordinary C function calls conflict with this event loop.
Re:Grumpy: Go running Python by dsymonds on Wednesday January 04, 2017 @06:51PM (#13323303)
Segmented stacks in Go went away in Go 1.3 (https://golang.org/doc/go1.3#stacks; June 2014).

The alternate stack structure is indeed one issue. The bigger one is the GC, though; the Go runtime needs to know which pointers it is responsible for freeing, and which are the responsibility of the C code.

Re:Grumpy: Go running Python by masklinn on Thursday January 05, 2017 @06:24AM (#13326300)
> The bigger one is the GC; the Go runtime needs to know which pointers it is responsible for freeing

That is not the bigger issue, and AFAIK already handled for C types.

The stack/calling conventions is the reason why cgo is "not go", cgo calls have significantly more overhead than just about every other FFI (the overhead of a cgo call is ~2 orders of magnitude more than a "native" go call, or was around the same time last year, that is you could perform ~100 no-op non-inlined native calls to a do-nothing function by the time you need for a single cgo call to the same).

Re:Grumpy: Go running Python by dsymonds on Thursday January 05, 2017 @08:55PM (#13333287)
The original question was about why C extensions are not supported by Go. It's not a matter of performance; it's a matter of correctness.
Re:Grumpy: Go running Python by vram22 on Wednesday January 04, 2017 @01:40PM (#13320883)
>Additionally, Go code runs inside an event loop, which enables excellent I/O performance without kernel context-switches

Interesting, didn't know this (that Go code runs in an event loop). Is the reason something to do with goroutines and channels? something like, a routine gets info that data is available for it to read (on a channel, sent by another goroutine), via an event it receives?

Also, can you explain this point:

"which enables excellent I/O performance without kernel context-switches" ?

Re:Grumpy: Go running Python by takeda on Wednesday January 04, 2017 @03:48PM (#13321898)
Since we also talking about Python. If you ever used AsyncIO you will see that programming in it is a bit different than you usually write code without it.

Before you can call any coroutine you first need to start an event loop and schedule something in it. This essentially enables the language to schedule another async function each time you use await.

Since Go by default always is async, before your main function is called, it sets up the even loop and then calls your main, which technically is also a coroutine. Your code appears to be sequential, but it is not executed that way.

Re:Grumpy: Go running Python by vram22 on Wednesday January 04, 2017 @05:41PM (#13322805)
Interesting ...
Re:Grumpy: Go running Python by moosingin3space on Wednesday January 04, 2017 @02:56PM (#13321507)
One of the reasons an asynchronous I/O event loop can be faster than a threaded model is that the CPU spends more of its time in a single userspace thread per core, switching between clients that are ready. A threaded server will incur a kernel-space context switch each time, while an asynchronous loop will keep the processing time in userspace.
Re:Grumpy: Go running Python by theatrus2 on Wednesday January 04, 2017 @01:25PM (#13320713)
Go has cgo, which works fine for most purposes of fine; native code interop is not an issue for Go.

Grumpy likely doesn't support the C extensions due to time, and complexity of having to actually emulate the GIL since Python does not have fine grained locking for structures. C extensions that work with Python data structures need to first hold the GIL.

Re:Grumpy: Go running Python by elementry on Thursday January 05, 2017 @12:13AM (#13324936)
> Does anyone know the technical reasons why C extensions are not (at least easily, apparently) supported by Go?

It's because Python's C API is inherently non-thread safe. The API lacks passing an interpreter pointer as a parameter (as Lua's API does for example). So Python is forced to use a terrible thread local storage hack involving the Global Interpreter Lock to swap interpreter instances which is insanely inefficient and limits compute-bound programs to a single thread.

Python 3.x had a chance to fix the API and do away with the GIL once and for all, but inexplicably they did not. There was a misguided notion that C extensions between 2.x and 3.x could be interoperable.

Re:Grumpy: Go running Python by pjmlp on Wednesday January 04, 2017 @01:43PM (#13320897)
I never saw Python as anything more than a scripting language to portably automate tasks across UNIX and Windows environment, even back in the Zope days.

My experience with Tcl teached me to stay away from languages that don't have either a JIT or AOT compiler on their reference implementation.

Re:Grumpy: Go running Python by jszymborski on Wednesday January 04, 2017 @01:51PM (#13320993)
I think that that's rather dismissive of a language that runs huge web, scientific, and general purpose applications daily.
Re:Grumpy: Go running Python by pjmlp on Wednesday January 04, 2017 @02:49PM (#13321438)
It might be, but that it is usually a consequence of not knowing any better or making use of existing libraries.

Just like people learned 8-bit BASIC and went on to do business applications and games on it. I went Z80 ASM instead.

Personally I would only use Python for shell scripting and advise for using Julia instead.

Of course, others see it differently.

Re:Grumpy: Go running Python by JPKab on Wednesday January 04, 2017 @03:30PM (#13321775)
Julia? Look, I enjoy playing with Julia, but have you actually used it? Because most of the people I meet who turn up their nose at Python and say nice things about Julia haven't ever used Julia. It's got great potential but has some major gaps and warts, and is just plain dog shit slow for certain things compared to SciPy, which, you may not be aware, is basically C and Fortran code wrapped in Python API.
Re:Grumpy: Go running Python by tavert on Thursday January 05, 2017 @02:58AM (#13325527)
> is just plain dog shit slow for certain things compared to SciPy

Do you have real examples here or is this just FUD? SciPy is not known for using absolute state of the art algorithms or perfectly optimized implementations. It can be pretty easy to improve on the naively implemented or legacy pieces of SciPy, e.g. http://tullo.ch/articles/python-vs-julia/

Re:Grumpy: Go running Python by pjmlp on Wednesday January 04, 2017 @03:40PM (#13321834)
I do follow Julia development and I am aware that it isn't quite there, but at least their community does embrace JIT compilation, not like Python that PyPy is just yet another project, ignored by the reference implementation.

> SciPy, which, you may not be aware, is basically C and Fortran code wrapped in Python API.

Which for me personally means, that I would rather C and Fortran directly or better yet, a C++, .NET or Java binding to them.

Re:Grumpy: Go running Python by orf on Wednesday January 04, 2017 @07:30PM (#13323595)
> Which for me personally means, that I would rather C and Fortran directly or better yet, a C++, .NET or Java binding to them.

Being able to describe things with a syntax that looks almost like pseudocode and runs highly-optimized C/Fortran code to do heavy lifting has huge, huge advantages.

Re:Grumpy: Go running Python by pjmlp on Thursday January 05, 2017 @02:19AM (#13325374)
Scala, Clojure, F# also allow it, with the added benefit of industrial strength JIT/AOT compilers.
Re:Grumpy: Go running Python by orf on Thursday January 05, 2017 @04:51AM (#13325957)
Except without the great Python ecosystem and without great projects like Numpy/Scipy
Re:Grumpy: Go running Python by pjmlp on Thursday January 05, 2017 @08:18AM (#13326850)
Numpy/Scipy are only relevant to a minor set of computer users, and even then, there are alternatives like LANPACK and BLAS.

Also Java and .NET ecosystems are just a little bigger than Python.

Re:Grumpy: Go running Python by orf on Thursday January 05, 2017 @10:15AM (#13327685)
> there are alternatives like LANPACK and BLAS.

LANPACK + BLAS = scipy

> Numpy/Scipy are only relevant to a minor set of computer users..

> ... Java and .NET ...

Being able to describe things with a syntax that looks almost like pseudocode and runs highly-optimized C/Fortran code to do heavy lifting has huge, huge advantages.

Re:Grumpy: Go running Python by pjmlp on Thursday January 05, 2017 @10:29AM (#13327845)
> LANPACK + BLAS = scipy

Thanks, I already knew that.

> Being able to describe things with a syntax that looks almost like pseudocode and runs highly-optimized C/Fortran code to do heavy lifting has huge, huge advantages.

Hence we are back at Scala, Clojure, F#, enjoying the respective AOT/JIT native code compilers, and integrating with that highly-optimized C/Fortran code.

Re:Grumpy: Go running Python by JPKab on Tuesday January 10, 2017 @11:32AM (#13366071)
I use Scala and Clojure. I like F#. Have you ever worked at an actual business that needs to hire developers that know these languages WELL? I can tell you that the significant added cost can't be justified by the not-very-big benefits compared to just using Scipy.
Re:Grumpy: Go running Python by orf on Thursday January 05, 2017 @10:58AM (#13328128)
> Scala, Clojure, F#

Functional languages. The "year of Linux on the desktop" of programming languages. Also none of those look like pseudocode (Scala does if you ignore bits and squint).

Re:Grumpy: Go running Python by pjmlp on Thursday January 05, 2017 @01:56PM (#13330186)
The amount of job postings say otherwise.
Re:Grumpy: Go running Python by orf on Saturday January 07, 2017 @09:22AM (#13344251)
No they don't. On stack overflow jobs F# has 14 posts, closure 28 and Scala ~100. Python has 400+.

Other sites have similar totals. But they are filtering for London/UK/Europe, so that might be a bias.

Re:Grumpy: Go running Python by petters on Wednesday January 04, 2017 @03:52PM (#13321920)
This made be wary of Julia: http://danluu.com/julialang/
Re:Grumpy: Go running Python by goatlover on Wednesday January 04, 2017 @02:59PM (#13321527)
Julia's nice, but it's playing catch up to R & Python. Name any statistical algorithm, and R probably has it. Name any scientific field, and there's probably a Python library for it.
Re:Grumpy: Go running Python by wolf550e on Wednesday January 04, 2017 @03:15PM (#13321670)
Some people are very productive in Django and Rails (and the like), in non-performant languages. Many sites never grow to the size where running on the JVM (or AOT-compiled Go) would save you money from fewer servers vs. more coder-hours.
Re:Grumpy: Go running Python by goatlover on Wednesday January 04, 2017 @02:57PM (#13321519)
The scientific community thinks otherwise.
Re:Grumpy: Go running Python by jerf on Wednesday January 04, 2017 @03:05PM (#13321585)
The scientific community uses it as a tool to automate specification of tasks to be performed inside of C libraries. It's a case pjmlp may not have explicitly named, but it's of the same kind.
Re:Grumpy: Go running Python by lima on Wednesday January 04, 2017 @03:19PM (#13321701)
PyPy is making good progress at implementing and JITing numpy as native Python code.
Re:Grumpy: Go running Python by simonster on Wednesday January 04, 2017 @05:29PM (#13322712)
They've been working on NumPyPy since at least 2011, and to my knowledge it's still not in serious use.
Re:Grumpy: Go running Python by viraptor on Wednesday January 04, 2017 @05:01PM (#13322497)
AOT can be done by Cython and it's fully compatible with reference as far as I know.