News for hackers. Stuff that matters.
Re:Nuitka: a Python compiler written in Python by samsquire on Thursday June 17, 2021 @09:43AM (#27539467)
I love Python so much. It's my favourite language. It's so easy.

I don't even use the dynamic nature of Python - monkey patching etc.

I wish more people wrote algorithms in Python for understandability then compiled them into a compiled language for performance. I find that Python resembles pseudocode because it is syntactically sparse, unlike C++. In Python I don't need to worry about ownership or memory. It probably needs Pointers to be a low level system language.

I just wish Python had parallelism.

Re:Nuitka: a Python compiler written in Python by lmm on Thursday June 17, 2021 @10:38AM (#27540046)
I used to take your view, but actually there are plenty of languages (e.g. ML-family languages) where you get the best of both worlds - light, clean syntax, but also type safety and decent performance.
Re:Nuitka: a Python compiler written in Python by garyrob on Thursday June 17, 2021 @12:30PM (#27541457)
I experimented with OCaml and some of my Python code. I found that using Python with Numba enabled me to get much better performance than the same algorithm written in OCaml.

In this case, using Numba for the performance-critical aspect was trivially simple, just the addition of a decorator. And I've found that is generally pretty close to true, at least for the kinds of things where I care about performance.

That's why I'm still in the Python world rather than the OCaml world for my algorithmic work. I really like OCaml as a language and would actually prefer to use it, but Python/Numba seems to give me substantially better performance, as well as all of Python's standard libraries.

And Numba lets you turn off the GIL, so you can get multiple cores going at once.

YMMV, of course. I'm not making a general claim that would be true for everyone. But I think it may be worth mentioning that it could be true for more uses than one might assume.

Re:Nuitka: a Python compiler written in Python by adsharma on Thursday June 17, 2021 @10:56AM (#27540243)
I'm sympathetic to this point of view. But then there is 25 yrs of stdlib that's tattooed onto people's brains and stackoverflow.

A pure python implementation of python stdlib ready be transpiled could be that bridge.

Also it's possible to support multiple source syntaxes in such an ecosystem. ML based, python based or even a hybrid. In the end all I need is a well documented AST.

Re:Nuitka: a Python compiler written in Python by willseth on Thursday June 17, 2021 @12:14PM (#27541268)
One of the main reasons people choose Python is the huge stdlib and vast package ecosystem. This is difficult to ignore if you need to do some very heavy lifting.
Re:Nuitka: a Python compiler written in Python by tenacious_tuna on Thursday June 17, 2021 @10:51AM (#27540186)
Can you share some examples? I'm very interested in a typesafe alternative to python, but low friction is also necessary.
Re:Nuitka: a Python compiler written in Python by lmm on Thursday June 17, 2021 @09:03PM (#27546261)
Personally I moved to Scala, which has a poor reputation because of some symbol-heavy libraries, but you can use it to write very clean Python-like code. Version 3 has actually added a more Python-inspired syntax with colons and indentation instead of braces.

Standard ML was where I started. As a language it's great, as an ecosystem it's limited. OCaml seems to be where the action is (and even then its packaging/dependency management isn't great - but then it can't be worse than Python's).

F# has a very good reputation but I haven't used it a lot myself.

Re:Nuitka: a Python compiler written in Python by tooltower on Thursday June 17, 2021 @11:18AM (#27540527)
They said ML family, so that's usually OCaml and SML.
Re:Nuitka: a Python compiler written in Python by nwatson on Thursday June 17, 2021 @11:38AM (#27540785)
I use JetBrains PyCharm with type annotations in my code. I run the type-checker/linter ("Inspect Code") every 10 minutes or so, it's muscle-memory at this point. It covers most cases, problem resolved.
Re:Nuitka: a Python compiler written in Python by nestorD on Thursday June 17, 2021 @11:21AM (#27540569)
F# comes to mind. It uses indentation instead of braces and has a nice ecosystem.
Re:Nuitka: a Python compiler written in Python by adsharma on Thursday June 17, 2021 @10:57AM (#27540257)
Why do you think python3 is not typesafe?
Re:Nuitka: a Python compiler written in Python by packetlost on Thursday June 17, 2021 @11:08AM (#27540391)
Because it's not? It has a type system but it's still duck-typed at the end of the day, and no amount of Mypy will change that.
Re:Nuitka: a Python compiler written in Python by adsharma on Thursday June 17, 2021 @11:30AM (#27540673)
It should still be possible to use the python syntax and a reimplemented stdlib to build a statically typed, typesafe variant.

I think you object to the name python3, because there is a duck typed interpreter that's popular.

Re:Nuitka: a Python compiler written in Python by packetlost on Thursday June 17, 2021 @11:38AM (#27540789)
You fundamentally cannot build a statically typed variant of Python without cutting portions of the stdlib, most notably `mock.patch`. You could do a good majority of it, probably enough to be completely usable, but at what point does it stop being Python? Go use Nim if you want statically typed Python-ish syntax.
Re:Nuitka: a Python compiler written in Python by adsharma on Thursday June 17, 2021 @01:33PM (#27542169)
py2many transpiles py3 to nim. Think of python as a way to interpret nim code if you like, with a large installed base of existing libraries.

There is demand for iterative creation of software: first get the logic out with the least friction and then think about types, resource leaks, good software engineering practices etc.

Re:Nuitka: a Python compiler written in Python by goodpoint on Friday June 18, 2021 @08:58AM (#27549808)
Re:Nuitka: a Python compiler written in Python by adsharma on Saturday June 19, 2021 @07:02PM (#27565327)
https://github.com/adsharma/py2many

  pip3 install py2many
  py2many --nim=1 foo.py
Re:Nuitka: a Python compiler written in Python by hermitdev on Friday June 18, 2021 @01:32PM (#27552752)
Type safety and static typing are orthogonal. Both C and C++ are statically typed, yet are not type safe, for instance.
Re:Nuitka: a Python compiler written in Python by metasyn on Thursday June 17, 2021 @11:17AM (#27540514)
nim-lang.org might interest you!
Re:Nuitka: a Python compiler written in Python by throwaway894345 on Thursday June 17, 2021 @04:11PM (#27544029)
Note that type safety typically means better tooling that what Python people are used to. If you’ve only used Python you have no idea what you’re missing out on with respect to refactoring, editor integrations, documentation generation, etc. And then there is the actual documentation for humans that is guaranteed to be correct and up to date so long as your build is green.
Re:Nuitka: a Python compiler written in Python by ibic on Thursday June 17, 2021 @11:16AM (#27540504)
Agree, just that ML-family languages take quite long for an average mortal like me to grok.
Re:Nuitka: a Python compiler written in Python by quietbritishjim on Thursday June 17, 2021 @09:52AM (#27539559)
(Standard reponse)

Python does support parallelism, so long as you're calling into a C library to do the work, because they (usually) release the GIL while doing the work. That sounds like a cop out but using C libraries is very common to do CPU intensive work anyway. Examples include numpy (e.g. you can call np.dot from multiple threads in Python and they will genuinely use multiple cores) and all the rest of the scipy stack, and even modules in the standard library (e.g. when using zipfile, even to process an in-memory buffer, the GIL is released).

Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @10:40AM (#27540068)
if you call np.dot on the same underlying ndarray from multiple threads, what are the semantics?

I don't know what they are but I think most people want a single invocation of np.dot in a single thread to use multiple worker threads (as described in https://scipy.github.io/old-wiki/pages/ParallelProgramming) by pushing the work to a thread-aware matrix library.

Re:Nuitka: a Python compiler written in Python by quietbritishjim on Thursday June 17, 2021 @12:07PM (#27541175)
> if you call np.dot on the same underlying ndarray from multiple threads, what are the semantics?

Usually, this will be safe because np.dot only reads from its arguments, and produces a fresh new array to hold the result.

I just checked the documentation and found there is an out=argument to pass a destination array. (In any case there are other numpy functions that do modify their argument.) If you pass the same array to that parameter in two different threads then I don't know what happens. Maybe numpy locks the GIL to write its output. Maybe it produces C-style undefined behaviour (this would be my guess ... and my preference).

> I think most people want a single invocation of np.dot in a single thread to use multiple worker threads

Maybe? I'm not convinced that's what I want! But it's pretty irrelevant either way to my point; np.dot was just an example. My real point is that lots of CPU-heavy Python functions are implemented in C and release the GIL, so in practice parallelism with Python is often possible.

Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @08:37PM (#27546067)
I've worked in this field awhile.

99% of people want this: np.dot and others use all the cores on your system by default. This accelerates the common use case, which is individual user on a multicore machine who wants single result ASAP. Then, there's an env var that lets you configure the underlying matrix library to use only a single (or maybe another number) of cores, and you work with the multithreaded/multiprocess libraries to run many independent calculations (possibly on shared read-only source data); then that scheduler has a tunable set to saturate the cores or IO on the system.

This is what most BLASses use and it's how data analysts and supercomputer folks can get along.

Re:Nuitka: a Python compiler written in Python by quietbritishjim on Friday June 18, 2021 @05:54AM (#27548852)
To reiterate: None of this contradicts my main point that numpy.dot releases the GIL, which it does even if it calls into a BLAS library that uses multiple cores (although of course that would then have its own global lock - potentially even an interprocess lock - to manage concurrent uses). numpy.dot was only meant to be an example in the first place! I could just as easily have talked about some optimisation routine in scipy or something in the standard library (actually I did mention ZipFile.extract).

> I've worked in this field awhile.

This statement undermines your point more than it supports it. There is no "this field" for numpy. Just working in some field that uses it is no qualification for knowing whether users as a whole would like it multithreaded automatically. "Data analysts" and "supercomputer folks" no doubt include many users but certainly not all and possibly not even most (for all we know).

> 99% of people want this: ...

I can believe this might be true. I don't think I ever claimed otherwise. In fact I said "maybe"! I only claimed that I don't want this.

I will make one more claim though: I don't think there's any way that you could know this, even if it's really true.

But yes, I agree and already knew that it's how BLAS libraries work by default, including OpenBLAS which is what the prebuilt numpy wheels use. Even then, as you're probably already aware by the sounds of it, that's only for sufficiently large matrices. If you're just using small matrices and vectors (e.g. in 3D to represent real-world coordinates) then processing will be done directly in the calling thread (because the overhead of using multiple threads would be greater than the benefit).

Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @10:52AM (#27540201)
Hi, would the downvoter mind adding a comment explaining why? What I said is technically correct so it's unclear what the objection is.
Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @11:14AM (#27540467)
No need to downvote a request for explanation; this is within HN rules. Otherwise, you're giving an ambiguous signal so I don't know whether you're unhappy with the current state of Python, or disagree with my technical statement (in which case, please provide a counterargument instead of a vote).
Re:Nuitka: a Python compiler written in Python by pitaj on Thursday June 17, 2021 @11:17AM (#27540510)
FYI complaining about votes is against HN comment policy.
Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @11:43AM (#27540864)
I'm not complaining about votes. I'm asking for people to clarifying why they are downvoting a technically correct statement.
Re:Nuitka: a Python compiler written in Python by dragonwriter on Thursday June 17, 2021 @12:22PM (#27541383)
Requesting explanation of downvotes is among the things the policy against discussion of the votes on a post is directed at, and, IMO, also demonstrates a fundamental failure to understand (or respect) the purpose of downvotes, which is about managing signal-to-noise ratio by, among other things, not polluting threads with meta-level discussion (abd invariably debates) of why a particular comment is (or is not) inappropriate.
Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @08:34PM (#27546041)
I have 11,659 karma. I'm not bragging but that means I have a pretty good understanding and respect for the purpose of downvotes (because I learn from them how to make more upvotes). Nothing is being polluted here, just a reasonable request for clarification (for example I would love to learn that I was factually wrong!). I'm guessing the downvote signal was actually somebody agreeing with me and showing displeasure with the problem, or believing I was technically incorrect, or gauche to be pointing out the problem in the first place.
Re:Nuitka: a Python compiler written in Python by garyrob on Thursday June 17, 2021 @12:34PM (#27541509)
"Python does support parallelism, so long as you're calling into a C library to do the work"

Actually, you can use Numba to get the same effect while still writing Python. Numba allows you to simply apply a decorator to critical Python code and it is JITted into C-like performance. I do this in my code, and it has worked great for my uses.

Re:Nuitka: a Python compiler written in Python by amelius on Thursday June 17, 2021 @10:31AM (#27539972)
Not really. You can't build large data structures (not arrays, but trees, graphs, etc) with structural sharing between threads. Unless of course you translate all of your data to C too, at which point you are really doing everything in C (data and code).
Re:Nuitka: a Python compiler written in Python by quietbritishjim on Thursday June 17, 2021 @12:15PM (#27541287)
OK, let's put it like this: In some contexts, but not all, Python supports a useful amount of parallelism.

I have definitely worked on Python projects in the past that were easily parallisable and worked absoluately fine. I simply created threads with threading.Thread, passed messages around with queue.Queue and used some standard Python modules to process those messages. The messages were independent enough from each other that I didn't have any issues with marshalling access to them, while being large enough that the overhead of mulithreading was still outweighed by its benefits. Rewriting the whole thing in C++ (etc.) would have made negligable difference to performance.

I can certainly imagine a program similar to what you describe, where there are lots of messages that operate on a single monolythic data structure, which couldn't easily be made to work with true parallelisation in Python.

Re:Nuitka: a Python compiler written in Python by incrudible on Thursday June 17, 2021 @10:59AM (#27540272)
> You can't build large data structures (not arrays, but trees, graphs, etc) with structural sharing between threads.

You probably do not want to do that in the first place. The reason that the GIL still exists is that the overhead of fine-grained locking for Python datastructures would negate the speedup from multithreading.

Re:Nuitka: a Python compiler written in Python by amelius on Thursday June 17, 2021 @11:41AM (#27540836)
Which is why Python doesn't really support parallelism.

Other language (like GoLang) do support parallelism but they have a more advanced runtime and don't use a GIL.

Re:Nuitka: a Python compiler written in Python by incrudible on Thursday June 17, 2021 @02:42PM (#27542976)
It's not specific to Python. If you want to do fine-grained "parallelism" on a set of complex shared datastructures, your cores might look busy, but really you're just wasting most of the time locking/unlocking stuff. That's not necessarily going to result in a speedup over a lock-free single-threaded implementation (that you didn't implement to compare against).

In the case of Python, the single-threaded implementation was already there, the implementation that removed the GIL in favor of fine-grained locking failed to deliver the goods.

If you want to actually take advantage of parallelism, you want data structures that are amenable to it, which is basically sections of arrays without a lot of potential read/write conflicts over multiple threads. If you're at that point, you probably want to use C/C++/Rust anyway, you don't want speed up something that's already 100x slower (the interpreter) by parallelizing it. Python offers that with C-extensions like NumPy.

Re:Nuitka: a Python compiler written in Python by amelius on Friday June 18, 2021 @02:33AM (#27547905)
> your cores might look busy, but really you're just wasting most of the time locking/unlocking stuff.

Not if you're reading most of the time and are using a modern concurrent GC.

Re:Nuitka: a Python compiler written in Python by incrudible on Friday June 18, 2021 @08:03AM (#27549470)
It doesn't matter if you're reading or writing, if there's a potential conflict you need to lock, or you need some complex and probably buggy "lock-free" data structure. The complexity and memory overhead of a concurrent GC just goes on top of that.
Re:Nuitka: a Python compiler written in Python by quietbritishjim on Sunday June 20, 2021 @01:32PM (#27572165)
Locking an uncontended lock is cheap (at least compared to a contended one) so actual conflict does matter more than potential conflict.
Re:Nuitka: a Python compiler written in Python by adsharma on Thursday June 17, 2021 @10:10AM (#27539749)
Please try py2many, which targets this use case. Go, rust, C++ are among the 7 supported backends.

http://github.com/adsharma/py2many

Re:Nuitka: a Python compiler written in Python by O_H_E on Thursday June 17, 2021 @11:31AM (#27540684)
That is Julia for you: https://julialang.org/blog/2012/02/why-we-created-julia/

\s? :D

I understand that you (and I for that matter) can't stop using python altogether rn. Just wanted to point out that these two desirable qualities where build into Julia from the get go.

Re:Nuitka: a Python compiler written in Python by kortex on Thursday June 17, 2021 @12:52PM (#27541699)
Julia just isn't a replacement for Python yet. Recently did a deep dive researching various capabilities that would make Julia at least useful as a microservice/RPC target for a more I/O heavy language like Python. gRPC support is really poor. There's some OpenAPI/Swagger tooling, but it's not great. Likewise for ZeroRPC. The best candidate is probably the WebApi library [0] but even then, it does not inspire a ton of confidence for production. From cruising github issues and the Julia forums, nothing in this space (rpc/io/microservice) feels really bulletproof. Combined with no solid native executable support, it's just a nonstarter for me.

My gestalt sensation is the community is still too small, the tooling too unpolished, to be ready for anything production-worthy. If there were a bulletproof 0rpc library, I think it would go a huge way towards growing the community and mindshare through successive approximation. But the other problem is many of the folks on the forum seem totally disinterested in this problem.

[0] https://github.com/JuliaWeb/JuliaWebAPI.jl

Re:Nuitka: a Python compiler written in Python by pjmlp on Thursday June 17, 2021 @10:23AM (#27539887)
You can do low level coding via ctypes, and on Micro/CircuitPython you can even mess with Assembly, just like on some old BASIC dialects.
Re:Nuitka: a Python compiler written in Python by pansa2 on Thursday June 17, 2021 @10:34AM (#27539992)
> I just wish Python had parallelism.

Would support for parallelism have benefits other than increasing speed? If not, I'd rather see an increase in Python's single-threaded performance - which is what Guido and his colleagues at Microsoft are currently working on.

Re:Nuitka: a Python compiler written in Python by samsquire on Thursday June 17, 2021 @10:40AM (#27540067)
The problem with interpreting the Python way, using a switch loop ceval.c is that there is plenty of overhead with that loop. Branch prediction fails. It could be every opcode is compared before reaching the actual opcode in the switch.

I wonder if Python will ever take the JVM approach of having a TemplateInterpreter and mapping each bytecode to assembly directly. Of course you would have to do it for each platform.

http://openjdk.java.net/groups/hotspot/docs/RuntimeOverview....

Re:Nuitka: a Python compiler written in Python by divbzero on Thursday June 17, 2021 @09:47AM (#27539511)
By parallelism do you mean something different from multiprocessing? [1]

[1]: https://docs.python.org/3/library/multiprocessing.html

Re:Nuitka: a Python compiler written in Python by samsquire on Thursday June 17, 2021 @09:53AM (#27539574)
I want Thread to run in parallel.

I've tried multiprocessing but I found it buggy for my use case.

I tried to create a worker queue and submit items to worker threads with JoinableQueue but eventually the system deadlocks and no progress is made. I'm not sure what the problem is. It never finishes.

It's a sentence correlator using multiprocessing - it generates correlations of words in your sentences.

https://github.com/samsquire/notebook/blob/master/sentence-c... https://github.com/samsquire/notebook/blob/master/workers.py

Maybe somebody can catch what I'm doing wrong.

edit: it may be because i've forgotten to run task_done on joinable queue. edit2: yes it was that! it works!

Re:Nuitka: a Python compiler written in Python by aw1621107 on Thursday June 17, 2021 @10:58AM (#27540268)
Out of curiosity, is using multiprocessing.Pool’s map/etc.() methods not an option? It’s a higher-level API which would remove the need to manually manage workers/a queue.
Re:Nuitka: a Python compiler written in Python by triceratops on Thursday June 17, 2021 @09:51AM (#27539551)
Pretty sure they're referring to the GIL.[1]

1. https://realpython.com/python-gil/

Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @10:41AM (#27540086)
yes, for many of us, we really do want a CPython that runs multiple threads of the same interpreter, with memory sharing. That said after decades of CPython not supporting it, I've rewritten most of my code to use multiprocessing with granular parallelism.