mirror of
https://github.com/vacp2p/nim-libp2p-experimental.git
synced 2025-01-12 03:14:15 +00:00
a4090c7382
* update readme and organize the example folder * adding package list * add packages done * basic readme done * fix the go-daemon example folder * add go-daemon folder in readme * fix readme icon * add badges * add nim min version * Update README background Co-Authored-By: Dmitriy Ryajov <dryajov@gmail.com> * fix all the comments * Update README.md wording Co-Authored-By: Dmitriy Ryajov <dryajov@gmail.com> * fix file path in examples/ * add comments to example * add comments to directchat and fix start.nim * remove unnecessary modules from directchat * del customdata * improve directchat * finish second.nim * removea gcsafe * with err * change var to let * remove final.nim * fix comments on pull request * fix comments on pull request * replace result with return * add hint when start and exit command * update output string * fix above comments * add api documentation * fix readme format * update readme format * readme table of content done * fix format * fix format * include links to the tutorial article Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
39 lines
1.1 KiB
Nim
39 lines
1.1 KiB
Nim
when not(compileOption("threads")):
|
|
{.fatal: "Please, compile this program with the --threads:on option!".}
|
|
|
|
import chronos # an efficient library for async
|
|
|
|
proc processInput(rfd: AsyncFD) {.async.} =
|
|
echo "Type something below to see if the multithread IO works:\nType 'exit' to exit."
|
|
|
|
let transp = fromPipe(rfd)
|
|
while true:
|
|
let a = await transp.readLine()
|
|
|
|
if a == "exit":
|
|
quit(0);
|
|
|
|
echo "You just entered: " & a
|
|
|
|
proc readInput(wfd: AsyncFD) {.thread.} =
|
|
## This procedure performs reading from `stdin` and sends data over
|
|
## pipe to main thread.
|
|
let transp = fromPipe(wfd)
|
|
|
|
while true:
|
|
let line = stdin.readLine()
|
|
discard waitFor transp.write(line & "\r\n")
|
|
|
|
proc main() {.async.} =
|
|
let (rfd, wfd) = createAsyncPipe()
|
|
if rfd == asyncInvalidPipe or wfd == asyncInvalidPipe:
|
|
raise newException(ValueError, "Could not initialize pipe!")
|
|
|
|
var thread: Thread[AsyncFD]
|
|
thread.createThread(readInput, wfd)
|
|
|
|
await processInput(rfd)
|
|
|
|
when isMainModule: # isMainModule = true when the module is compiled as the main file
|
|
waitFor(main())
|
|
|