Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
958 views
in Technique[技术] by (71.8m points)

linux - BASH: Best architecture for reading from two input streams

Simple script here:

a) constantly read from a socket and store values in an associative array
b) constantly read values from stdin and respond t/f if they already exist in the associative array

a & b are random events, not associated in any way.

The trick is accessing the array from both subprocesses (since putting a process in the background spawns it as a subprocess)

I'm thinking through the best strategy, and a few ideas occur, but I wonder if anyone has anything better in mind:

1) redirect the input from socket to stdin as a subprocess and handle both inputs in one while loop (data sizes are small, <30 characters, so I guess they will remain atomic?).
2) read the socket, then STDIN with small (0.1?) timeout values on read so as to mimic non-blocking I/O.
3) UPDATE: write the socket data to a file (actually have the other process write it directly to a file), then each time a request comes in to check if the value exists, process the entries in the file, adding them to the array (use file locking).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Bash is not the right tool for this. This problem is typically solved using the select(2) or poll(2) system calls, which allow you to wait on multiple file descriptors simultaneously without spinning. Bash does not have an interface to either of those.

I'd recommend using a scripting language such as Python or Perl (whatever you're comfortable with, really) that provides an interface with select or poll (e.g. Python's select module).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...