you are viewing a single comment's thread
view the rest of the comments
[–] 148 points 1 year ago (4 children)

all programs are single threaded unless otherwise specified.

  • source
  • hideshow 8 child comments
  • [–] 49 points 1 year ago (3 children)

    It’s safe to assume that any non-trivial program written in Go is multithreaded

  • source
  • parent
  • hideshow 6 child comments
  • [–] 16 points 1 year ago (2 children)

    And yet: You’ll still be limited to two simultaneous calls to your REST API because the default HTTP client was built in the dumbest way possible.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 1 year ago (1 child)

    The client object or the library?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago

    … Is this a trick question? The object, provided by the library (net/http which is about as default as they come) sets “DefaultMaxIdleConnsPerHost” to 2. This is significant because if you finish a connection and you’ve got more than 2 idles, it slams that connection close. If you have a lot of simultaneous fast lived requests to the same IP (say a load balanced IP), your go programs will exhaust the ephemeral port list quickly. It’s one of the most common “gotchas” I see where Go programs work great in dev and blow themselves apart in prod.

    https://dev.to/gkampitakis/http-connection-churn-in-go-34pl is a fairly decent write up.

  • source
  • parent
  • [–] 7 points 1 year ago (1 child)

    I absolutely love how easy multi threading and communication between threads is made in Go. Easily one of the biggest selling points.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago (1 child)

    Key point: they're not threads, at least not in the traditional sense. That makes a huge difference under the hood.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago* (1 child)

    Well, they're userspace threads. That's still concurrency just like kernel threads.

    Also, it still uses kernel threads, just not for every single goroutine.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago (1 child)

    What I mean is, from the perspective of performance they are very different. In a language like C where (p)threads are kernel threads, creating a new thread is only marginally less expensive than creating a new process (in Linux, not sure about Windows). In comparison creating a new 'user thread' in Go is exceedingly cheap. Creating 10s of thousands of goroutines is feasible. Creating 10s of thousands of threads is a problem.

    Also, it still uses kernel threads, just not for every single goroutine.

    This touches on the other major difference. There is zero connection between the number of goroutines a program spawns and the number of kernel threads it spawns. A program using kernel threads is relying on the kernel's scheduler which adds a lot of complexity and non-determinism. But a Go program uses the same number of kernel threads (assuming the same hardware and you don't mess with GOMAXPROCS) regardless of the number of goroutines it uses, and the goroutines are cooperatively scheduled by the runtime instead of preemptively scheduled by the kernel.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago*

    Great details! I know the difference personally, but this is a really nice explanation for other readers.

    About the last point though: I'm not sure Go always uses the maximum amount of kernel threads it is allowed to use. I read it spawns one on blocking syscalls, but I can't confirm that. I could imagine it would make sense for it to spawn them lazily and then keep around to lessen the overhead of creating it in case it's needed later again, but that is speculation.

    Edit: I dove a bit deeper. It seems that nowadays it spawns as many kernel threads as CPU cores available plus additional ones for blocking syscalls. https://go.dev/doc/go1.5 https://docs.google.com/document/u/0/d/1At2Ls5_fhJQ59kDK2DFVhFu3g5mATSXqqV5QrxinasI/mobilebasic

  • source
  • parent
  • [–] 23 points 1 year ago (3 children)

    Does Python have the ability to specify loops that should be executed in parallel, as e.g. Matlab uses parfor instead of for?

  • source
  • parent
  • hideshow 6 child comments
  • [–] 52 points 1 year ago (2 children)

    python has way too many ways to do that. asyncio, future, thread, multiprocessing...

  • source
  • parent
  • hideshow 4 child comments
  • [–] 41 points 1 year ago (1 child)

    Of the ways you listed the only one that will actually take advantage of a multi core CPU is multiprocessing

  • source
  • parent
  • hideshow 2 child comments
  • [–] 11 points 1 year ago (1 child)

    yup, that's true. most meaningful tasks are io-bound so "parallel" basically qualifies as "whatever allows multiple threads of execution to keep going". if you're doing numbercrunching in pythen without a proper library like pandas, that can parallelize your calculations, you're doing it wrong.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 8 points 1 year ago* (1 child)

    I’ve used multiprocessing to squeeze more performance out of numpy and scipy. But yeah, resorting to multiprocessing is a sign that you should be dropping into something like Rust or a C variant.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 9 points 1 year ago (1 child)

    I've always hated object oriented multi threading. Goroutines (green threads) are just the best way 90% of the time. If I need to control where threads go I'll write it in rust.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 7 points 1 year ago (2 children)

    nothing about any of those libraries dictates an OO approach.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 0 points 1 year ago (1 child)

    If I have to put a thread object in a variable and call a method on it to start it then it's OO multi threading. I don't want to know when the thread spawns, I don't want to know what code it's running, and I don't want to know when it's done. I just want shit to happen at the same time (90% of the time)

  • source
  • parent
  • hideshow 2 child comments
  • [–] 8 points 1 year ago (2 children)

    Are you still using matlab? Why? Seriously

  • source
  • parent
  • hideshow 4 child comments
  • [–] 18 points 1 year ago (1 child)

    No, I'm not at university anymore.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 5 points 1 year ago (1 child)
  • [–] 5 points 1 year ago* (last edited 1 year ago) (1 child)

    We weren't doing any ressource extensive computations with Matlab, mainly just for teaching FEM, as we've had an extensive collection of scripts for that purpose, and pre- and some post processing.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago

    I don't like that they don't write their own algorithms in any other language. I was trying to understand low-pass filters a while back and so many web pages were like, "Call this MATLAB function" or "here's a code generator that puts out bad C for specific filter parameters" Like no, I want the algorithm explained to me...

  • source
  • parent
  • [–] 7 points 1 year ago (1 child)

    I was telling a colleague about how my department started using Rust for some parts of our projects lately. (normally Python was good enough for almost everything but we wanted to try it out)

    They asked me why we're not using MATLAB. They were not joking. So, I can at least tell you their reasoning. It was their first programming language in university, it's safer and faster than Python, and it's quite challenging to use.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 14 points 1 year ago (1 child)

    I think OP is making a joke about python's GIL, which makes it so even if you are explicitly multi threading, only one thread is ever running at a time, which can defeat the point in some circumstances.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago* (last edited 1 year ago) (1 child)

    no, they're just saying python is slow. even without the GIL python is not multithreaded. the thread library doesn't use OS threads so even a free-threaded runtime running "parallel" code is limited to one thread.

    apparently not!

  • source
  • parent
  • hideshow 2 child comments
  • [–] 3 points 1 year ago (2 children)

    If what you said were true, wouldn't it make a lot more sense for OP to be making a joke about how even if the source includes multi threading, all his extra cores are wasted? And make your original comment suggesting a coding issue instead of a language issue pretty misleading?

    But what you said is not correct. I just did a dumb little test

    import threading 
    import time
    
    def task(name):
      time.sleep(600)
    
    t1 = threading.Thread(target=task, args=("1",))
    t2 = threading.Thread(target=task, args=("2",))
    t3 = threading.Thread(target=task, args=("3",))
    
    t1.start()
    t2.start()
    t3.start()
    

    And then ps -efT | grep python and sure enough that python process has 4 threads. If you want to be even more certain of it you can strace -e clone,clone3 python ./threadtest.py and see that it is making clone3 syscalls.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 2 points 1 year ago* (last edited 1 year ago) (1 child)

    Now do computation in those threads and realize that they all wait on the GIL giving you single core performance on computation and multi threaded performance on io.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 1 year ago (2 children)

    Correct, which is why before I had said

    I think OP is making a joke about python's GIL, which makes it so even if you are explicitly multi threading, only one thread is ever running at a time, which can defeat the point in some circumstances.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 1 year ago* (1 child)

    Isn't that what threading is? Concurrency always happens on single core. Parallelism is when separate threads are running on different cores. Either way, while the post is meant to be humorous, understanding the difference is what prevents people from picking up the topic. It's really not difficult. Most reasons to bypass the GIL are IO bound, meaning using threading is perfectly fine. If things ran on multiple cores by default it would be a nightmare with race conditions.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago

    I haven't heard of that being what threading is, but that threading is about shared resourcing and memory space and not any special relationship with the scheduler.

    Per the wiki:

    On a multiprocessor or multi-core system, multiple threads can execute in parallel, with every processor or core executing a separate thread simultaneously; on a processor or core with hardware threads, separate software threads can also be executed concurrently by separate hardware threads.

    https://en.m.wikipedia.org/wiki/Thread_(computing)

    I also think you might be misunderstanding the relationship between concurrency and parallelism; they are not mutually exclusive. Something can be concurrent through parallelism, as the wiki page has (emphasis mine):

    Concurrency refers to the ability of a system to execute multiple tasks through simultaneous execution or time-sharing (context switching), sharing resources and managing interactions.

    https://en.m.wikipedia.org/wiki/Concurrency_(computer_science)

  • source
  • parent