Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit f00801f

Browse files
committed
fix readme
1 parent 639ef54 commit f00801f

File tree

1 file changed

+74
-72
lines changed

1 file changed

+74
-72
lines changed

README.md

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -82,48 +82,50 @@ User(User code) -->|init supervisor & children|Sup(Supervisor 1)
8282
Sup-->|add child & run|Child2(Child 2 - Task 2)
8383
Sup-->|add child & run|Childn(Child N - Task N)
8484
```
85+
8586
(install extension support mermaid to view flow)
8687

8788
supervisor example:
8889

8990
```go
9091
// example function need to run in child.
9192
loop := func(a int) {
92-
for i := 0; i < a; i++ {
93-
time.Sleep(time.Second)
94-
fmt.Println("loop at", i)
95-
}
96-
fmt.Println("Loop exit...")
93+
for i := 0; i < a; i++ {
94+
time.Sleep(time.Second)
95+
fmt.Println("loop at", i)
96+
}
97+
fmt.Println("loop exit...")
9798
}
9899

99100
// example function run in child. It will panic if counter > 3.
100-
LoopWithPanic := func(a int, panicString string) {
101-
for i := 0; i < a; i++ {
102-
time.Sleep(time.Second)
103-
fmt.Println("loop at", i)
104-
if i > 3 {
105-
panic(panicString)
106-
}
107-
}
101+
loopWithPanic := func(a int, panicString string) {
102+
for i := 0; i < a; i++ {
103+
time.Sleep(time.Second)
104+
fmt.Println("loop at", i)
105+
if i > 3 {
106+
panic(panicString)
107+
}
108+
}
108109
// maybe you won't see this.
109-
fmt.Println("LoopWithPanic exit...")
110+
fmt.Println("loopWithPanic exit...")
110111
}
111112

112113
// create a supervisor.
113114
sup := easyworker.NewSupervisor()
114115

115116
// add direct child to supervisor.
116-
sup.NewChild(easyworker.ERROR_RESTART, Loop, 5)
117-
sup.NewChild(easyworker.NO_RESTART, LoopWithPanic, 5, "test panic")
117+
sup.NewChild(easyworker.ERROR_RESTART, loop, 5)
118+
sup.NewChild(easyworker.NO_RESTART, loopWithPanic, 5, "test panic")
118119

119120

120121
// create a child.
121-
child, _ := easyworker.NewChild(easyworker.ALWAYS_RESTART, LoopWithPanic, 5, "other panic")
122+
child, _ := easyworker.NewChild(easyworker.ALWAYS_RESTART, loopWithPanic, 5, "other panic")
122123

123124
// add exists child.
124125
sup.AddChild(&child)
125126

126-
//...
127+
// or do something you want.
128+
time.Sleep(15 * time.Second)
127129

128130
// stop all worker.
129131
// this function depends how long fun return.
@@ -148,15 +150,15 @@ Example:
148150
```go
149151
// basic func with context.
150152
loopWithContext := func(ctx context.Context, a int) {
151-
// get supervisor's id.
152-
supId := ctx.Value(easyworker.CTX_SUP_ID)
153-
// get child's id.
154-
childId := ctx.Value(easyworker.CTX_CHILD_ID)
155-
156-
for i := 0; i < a; i++ {
157-
fmt.Println("Sup: ", supId, "Child:", childId, "counter:", i)
158-
time.Sleep(time.Millisecond)
159-
}
153+
// get supervisor's id.
154+
supId := ctx.Value(easyworker.CTX_SUP_ID)
155+
// get child's id.
156+
childId := ctx.Value(easyworker.CTX_CHILD_ID)
157+
158+
for i := 0; i < a; i++ {
159+
fmt.Println("Sup: ", supId, "Child:", childId, "counter:", i)
160+
time.Sleep(time.Millisecond)
161+
}
160162
}
161163

162164
// create supervisor with context.
@@ -181,41 +183,41 @@ EasyTask example:
181183
```go
182184
// simple task.
183185
func sum(a ...int) (ret int) {
184-
for _, i := range a {
185-
ret += i
186-
}
187-
return ret
186+
for _, i := range a {
187+
ret += i
188+
}
189+
return ret
188190
}
189191

190192
func parallelTasks() {
191-
// number of workers.
192-
numWorkers := 3
193+
// number of workers.
194+
numWorkers := 3
193195

194-
// retry times.
195-
retryTimes := 0
196+
// retry times.
197+
retryTimes := 0
196198

197-
// sleep time before re-run.
198-
retrySleep := 0
199+
// sleep time before re-run.
200+
retrySleep := 0
199201

200-
// new config for EasyTask.
201-
config, _ := easyworker.NewConfig(sum, numWorkers, retryTimes, retrySleep)
202+
// new config for EasyTask.
203+
config, _ := easyworker.NewConfig(sum, numWorkers, retryTimes, retrySleep)
202204

203-
// new EasyTask.
204-
task, _ := easyworker.NewTask(config)
205+
// new EasyTask.
206+
task, _ := easyworker.NewTask(config)
205207

206-
// add tasks.
207-
myTask.AddTask(1, 2, 3)
208-
myTask.AddTask(3, 4, 5, 6, 7)
209-
myTask.AddTask(11, 22)
208+
// add tasks.
209+
myTask.AddTask(1, 2, 3)
210+
myTask.AddTask(3, 4, 5, 6, 7)
211+
myTask.AddTask(11, 22)
210212

211-
// start workers.
212-
r, e := myTask.Run()
213+
// start workers and get results.
214+
r, e := myTask.Run()
213215

214-
if e != nil {
215-
t.Error("run task failed, ", e)
216-
} else {
217-
fmt.Println("task result:", r)
218-
}
216+
if e != nil {
217+
t.Error("run task failed, ", e)
218+
} else {
219+
fmt.Println("task result:", r)
220+
}
219221
}
220222
```
221223

@@ -233,10 +235,10 @@ EasyStream example:
233235
```go
234236
// fun will do task
235237
fnStr = func(a int, suffix string) string {
236-
if a%3 == 0 {
237-
panic("panic from user func")
238-
}
239-
return fmt.Sprintf("%d_%s", a, suffix)
238+
if a%3 == 0 {
239+
panic("panic from user func")
240+
}
241+
return fmt.Sprintf("%d_%s", a, suffix)
240242
}
241243

242244
// input channel.
@@ -272,7 +274,7 @@ go func() {
272274
}()
273275

274276

275-
//...
277+
// do something.
276278

277279
// stop all worker.
278280
myStream.Stop()
@@ -292,24 +294,24 @@ Example 1:
292294

293295
```go
294296
loop := func(a int) {
295-
for i := 0; i < a; i++ {
296-
time.Sleep(time.Second)
297-
fmt.Println("loop at", i)
298-
}
299-
fmt.Println("Loop exit...")
297+
for i := 0; i < a; i++ {
298+
time.Sleep(time.Second)
299+
fmt.Println("loop at", i)
300+
}
301+
fmt.Println("Loop exit...")
300302
}
301303

302304
// create go task.
303305
g,_ := easyworker.NewGo(loop, 5)
304306

305307
go func() {
306-
// get a monitor to g.
307-
refId, ch := g.Monitor()
308+
// get a monitor to g.
309+
refId, ch := g.Monitor()
308310

309-
// get a signal when g done/failed.
310-
sig := <-ch
311+
// get a signal when g done/failed.
312+
sig := <-ch
311313

312-
fmt.Println("ref:", refId, "ok")
314+
fmt.Println("ref:", refId, "ok")
313315
}()
314316

315317
// start Go task.
@@ -332,10 +334,10 @@ g.Run()
332334
sig := <-ch
333335

334336
if sig.Signal != easyWorker.SIGNAL_DONE {
335-
// remove monitor link to Go.
336-
g.Demonitor()
337+
// remove monitor link to Go.
338+
g.Demonitor()
337339

338-
// retry one more.
339-
g.Run()
340+
// retry one more.
341+
g.Run()
340342
}
341-
```
343+
```

0 commit comments

Comments
 (0)