Imagine You have to mine some ores using gophers, you'll have Gary as our worker:
A common way of performing this task on single Threaded applications is by using Gary through all the stages of the mining, like this:
That's fine and all, but it really doesn't take advantage of maybe assigining different tasks to different workers, like this:
That's what we call concurrent programming, defined by:
In computer science, concurrency is the ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or at the same time simultaneously partial order, without affecting the final outcome.
While parallelism is defined by:
The term Parallelism refers to techniques to make programs faster by performing several computations at the same time. This requires hardware with multiple processing units. In many cases the sub-computations are of the same structure, but this is not necessary.
- Application Basic Logic:
flowchart LR
A[URL Status Checker] ---> B[http request]
B -.-> C[http://x-team.com]
B -.-> D[http://github.com]
B -.-> E[http://stackoverflow.com]
B -.-> F[http://google.com]
subgraph URLS
C
D
E
F
end
- This program has a natural blocking architecture:
flowchart LR
C[http://x-team.com]
D[http://github.com]
E[http://stackoverflow.com]
F[http://google.com]
subgraph URLS
C
D
E
F
end
C --> |wait|D
D --> |wait|E
E --> |wait|F
URLS --> A[need to receive the http response before moving to the next URL]
- Why not try to optimize this?
flowchart
C[http://x-team.com]
D[http://github.com]
E[http://stackoverflow.com]
F[http://google.com]
C -->|make request| X[return status]
D -->|make request| Y[return status]
E -->|make request| Z[return status]
F -->|make request| W[return status]
subgraph URLS
C
D
E
F
end
- Behind the Scenes
flowchart TB
subgraph Routines
A
B
C
D
E
W
Z
J
X
Y
end
A[CPU Core]<-->B[GO Scheduler]
B <-.-> C[Go Routine]
B <-.-> |pause/unpause|D[Go Routine]
B <-.-> E[Go Routine]
X[CPU Core]<-->Y[GO Scheduler]
Y <-.-> W[Go Routine]
Y <-.-> Z[Go Routine]
Y <-.-> J[Go Routine]
subgraph Running Program
A1[Main Routine]-->C1(Created when program is launched)
B1[Child Go Routine]-->D1
B2[Child Go Routine]-->D1
B3[Child Go Routine]-->D1
D1(Created by the go keyword)
end
- Why the child routines didn't run?
gantt
title Time -->
dateFormat DD-HH
axisFormat %L
section Program
main routine:active,crit,mr,01-00,5h
program exit: crit,after mr,4h
child go routine:active,mr2,01-02,6h
child go routine:active,mr2,01-02,7h
child go routine:active,mr2,01-02,5h
- The plan with Channels
flowchart TB
A[Main Routine] <--> C{Channel}
subgraph Routines
D[Child Go Routine]
E[Child Go Routine]
F[Child Go Routine]
end
D <-.-> C
E <-.-> C
F <-.-> C