Y
Y
yativ_sobb2021-04-02 10:37:59
go
yativ_sobb, 2021-04-02 10:37:59

Where can you use goroutines in Go?

I know this is a bit of a weird question, but I honestly didn't see where gorutine could be used in practice. All my tasks are related to an object and I rarely use arrays or lists, and if I do, there is not much data there and I can do without them.

I would like to know in what interesting tasks did you use goroutines?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Mamonov, 2021-04-02
@EvgenyMamonov

The simplest example is an HTTP server.
When an HTTP request comes to you, a goroutine is created and each request is quietly processed, no one is waiting for anyone. Those. the server can process several requests in parallel just at the expense of goroutines.
In fact, in almost all network services, it makes sense to use goroutines to parallelize request processing (except for epoll, kqueue, etc.)
Another good example.
You need to make an Avito parser :)) or a site indexer.
Those. the task boils down to the fact that you will need to constantly download pages from sites.
If you download one page after another, you will download for a very long time.
And so you can make a pool of several goroutines (at least 1 per processor thread) and get parallel page downloads.
To speed up the work of calculations (parallel computing), it makes sense to use goroutines only 1 per processor thread). Those. in cases where you have a huge amount of data and you need to speed up its processing as much as possible. For example, you have a 100GB file, and 8 cores in the processor - you can split the file into 8 parts and process these parts in parallel (in the simplest version, in real life you just need to read new data in parts and feed it to a free goroutine
) to run some task in the background.
For example, periodic cache cleaning/outdated entries in the database.
There are many more real examples :)
In general, it makes sense to use them where you need the parallel execution of a task or different tasks in parallel :)

R
Roman Mirilaczvili, 2021-04-02
@2ord

Everyone's tasks are different. Have you worked with streams? It looks like them.
Simple example:
There are 2 requests in a DB. One lasts, say, 3 seconds, the other 10 seconds. And there is no dependence of one on the other.
If you perform them sequentially, it will take 13 seconds. Too much. After scratching your forehead, you arrive at another solution:
run 2 requests at the same time, each request in a goroutine! After waiting for them to end, 10 seconds will pass. Saving!
Another example:
You are working with queues. You send task commands to one queue, and you receive their results to another. Of course, both the producer and the consumer must work independently of each other. How do you run them in Go?
That's right, two goroutines!

U
uvelichitel, 2021-04-02
@uvelichitel

Mainly for asyncIO, asynchronous I/O.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question