You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 23, 2023. It is now read-only.
proxy.onData "action1": result = action1(data) # ---- 8
41
51
# ...
42
-
asyncCheck proxy.poll()
52
+
asyncCheck proxy.poll() # ---- 9
43
53
44
54
# create threads
45
-
proxy.createThread("foo1", fooMain)
55
+
proxy.createThread("foo1", fooMain) # ---- 10
46
56
proxy.createThread("foo2", fooMain)
47
57
#...
48
58
@@ -56,18 +66,31 @@ when isMainModeul:
56
66
main()
57
67
```
58
68
69
+
1.`import threadproxy` will also `import json, asyncdispatch`. They are almost always used together.
70
+
2. Define an entry function for threads. If you don't want to declare thread on your own, the argument must be a `ThreadProxy`. Also note that the `{.thread.}` pragma must be present.
71
+
3. Define a handler for `action`. The handler function is responsible for both `send` and `ask` handling. A `nil` return value will be converted to `JNull`
72
+
4. Similar to `on`, but a template version that save you from typing `proc...` everytime. The argument `data` is injected and so the name `onData`
73
+
5. Default handler for all unregistered actions. There is also a templated version `onDefaultData` that work similarly.
74
+
6. Start processing messages on channels asychronously . This must be called exactly once, otherwise nothing will happen.
75
+
7. Create a `MainThreadProxy` with a name. `MainThreadProxy` is also a `ThreadProxy` with responsibilities to handle threads and channels.
76
+
8. Define handlers for MainThreadProxy similar to that in fooMain.
77
+
9. Start processing messages similar to that in fooMain.
78
+
10. Create thread with a name and entry function.
79
+
59
80
## Examples
60
81
61
82
Example 1: simple ask
62
83
63
84
```nim
64
-
import threadproxy, asyncdispatch
85
+
import threadproxy
65
86
66
87
proc workerMain(proxy: ThreadProxy) {.thread.} =
67
88
# register action handler
68
-
proxy.onData "double":
69
-
let x = data.getInt()
70
-
return %(2*x)
89
+
proxy.onData "sum":
90
+
var x = 0
91
+
for n in data:
92
+
x += n.getInt()
93
+
return %x
71
94
72
95
# start processing channel
73
96
waitFor proxy.poll()
@@ -80,8 +103,8 @@ proc main() =
80
103
proxy.createThread("worker_0", workerMain)
81
104
82
105
# ask worker_0 to double 10
83
-
let answer = waitFor proxy.ask("worker_0", "double", %10)
84
-
assert answer == %20
106
+
let answer = waitFor proxy.ask("worker_0", "sum", %[2,3,5,7])
0 commit comments