BASH:从两个输入流读最好的架构 [英] BASH: Best architecture for reading from two input streams

查看:123
本文介绍了BASH:从两个输入流读最好的架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的简单脚本:

不断地从一个插座和存储值的关联数组结果读
B)不断地从标准输入读取值和响应T / F,如果它们已经存在的关联数组

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是随机事件,不以任何方式有关。

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)从套接字的输入重定向到标准输入作为一个子进程,在一个同时处理输入while循环(数据大小小,和LT; 30个字符,所以我想他们会保持原子) 。结果
2)读取插座,然后STDIN小(0.1?)的超时值上读出,以便模仿非阻塞I / O。结果
3)更新:套接字数据写入一个文件(实际上有其他进程直接写入到一个文件),那么每一个请求进来检查,如果该值存在,过程中的条目时间该文件,将其添加到阵列(使用文件锁定)。

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).

推荐答案

Bash是不是这个合适的工具。这个问题是使用 选择(2) 调查(2) 系统调用,它允许您在多个文件描述符等待无纺同时进行。击不具有一个接口要么那些。

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.

我建议使用脚本语言如Python或Perl(无论你舒服,真的),提供了一个接口选择调查(如Python的 选择 模块)。

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).

这篇关于BASH:从两个输入流读最好的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆