不同语言的程序之间共享变量的事实上的标准是什么?
我从未接受过这方面的正式培训,所以我想知道他们在学校教什么(如果有的话)。 假设您有两个用两种不同语言编写的程序:C++ 和 Python 或其他一些组合,并且您想在同一台机器上共享一个不断更新的变量,您会使用什么以及为什么?信息不需要受到保护,但必须是同步的并且是可靠的。
例如。程序A将从硬件设备获取一个值,并每0.1ms更新一次变量X,我希望能够尽可能频繁地从程序B访问该X并获取最新值。程序 A 和 B 是用两种不同的(健壮的)语言编写和编译的。如何从程序 B 访问 X?假设我有 A 和 B 的源代码,并且我不想完全重写或移植它们中的任何一个。
到目前为止我见过的方法包括:
- 文件缓冲区 - 读取和写入 单个文件(例如 C:\temp.txt)。
- 创建包装器 - 从 A 到 B 或 B A.
- 内存缓冲区 - 指定一个特定的 内存地址(互斥体?)。
- 通过套接字的 UDP 数据包 - 还没有 尝试过但看起来不错。 防火墙?
抱歉,我只是把它扔在那里,我不知道这项技术的名称是什么,所以我在搜索时遇到了麻烦。
I've never had formal training in this area so I'm wondering what do they teach in school (if they do).
Say you have two programs in written in two different languages: C++ and Python or some other combination and you want to share a constantly updated variable on the same machine, what would you use and why? The information need not be secured but must be isochronous should be reliable.
Eg. Program A will get a value from a hardware device and update variable X every 0.1ms, I'd like to be able to access this X from Program B as often as possible and obtain the latest values. Program A and B are written and compiled in two different (robust) languages. How do I access X from program B? Assume I have the source code from A and B and I do not want to completely rewrite or port either of them.
The method's I've seen used thus far include:
- File Buffer - Read and write to a
single file (eg C:\temp.txt). - Create a wrapper - From A to B or B
to A. - Memory Buffer - Designate a specific
memory address (mutex?). - UDP packets via sockets - Haven't
tried it yet but looks good.
Firewall?
Sorry for just throwing this out there, I don't know what the name of this technique is so I have trouble searching.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
那么您可以编写 XML 并使用一些基本的消息队列(如rabbitMQ)来传递消息
Well you can write XML and use some basic message queuing (like rabbitMQ) to pass messages around
不知道这是否有帮助,但我也是一名学生,这就是我认为你的意思。
我使用编组来获取 java 类并将其导入到 C# 程序中。
通过编组,您可以使用 xml 以某种方式传输代码,以便其他编码环境可以读取代码。
Don't know if this will be helpful, but I'm also a student, and this is what I think you mean.
I've used marshalling to get a java class and import it into a C# program.
With marshalling you use xml to transfer code in a way so that it can be read by other coding environments.
当提出特定问题时,您应该旨在提供尽可能多的信息。您已添加用例,但该用例不完整。
您的特定用例似乎是非常少量的数据,必须在 10kHz 高频下可用。我首先尝试确定是否可以真正使这两段代码成为单个进程的一部分,而不是两个不同的进程。根据语言(问题中遗漏),它甚至可能很简单,或者将不可能变成可能 - 取决于操作系统(问题中遗漏),调度程序可能不够快地从一个进程切换到另一个进程,并且可能会影响最新读取的可用性。线程之间的切换通常要快得多。
如果你不能把它们变成一个单一的进程,那么你将不得不使用一些短的IPC(进程间通信)。由于频率的原因,我会排除大多数重量级协议(避免 XML、CORBA),因为开销可能太高。如果接收端只需要访问最新值,并且访问频率可能低于 0.1 毫秒,那么您不想使用任何包含排队的协议,因为您不想读取下一个值。 em> 队列中的元素,您只关心最后,如果您没有在该元素良好时读取该元素,请避免在它已经过时时处理它的成本 - 即它不会循环从队列中提取并丢弃是有意义的。
我倾向于使用共享内存或内存映射共享文件(它们可能非常相似,取决于问题中缺少的平台)。根据元素的大小和确切的硬件架构(问题中缺少),您也许能够避免使用互斥锁进行锁定。作为当前英特尔处理器中的一个示例,如果变量正确对齐,则保证从内存中读取/写入 32 位整数的访问是原子的,因此在这种情况下您不会锁定。
When asking particular questions, you should aim at providing as much information as possible. You have added a use case, but the use case is incomplete.
Your particular use case seems like a very small amount of data that has to be available at a high frequency 10kHz. I would first try to determine whether I can actually make both pieces of code part of a single process, rather than two different processes. Depending on the languages (missing from the question) it might even be simple, or turn the impossible into possible --depending on the OS (missing from the question), the scheduler might not be fast enough switching from one process to another, and it might impact the availability of the latest read. Switching between threads is usually much faster.
If you cannot turn them into a single process, then you will have to use some short of IPC (Inter Process Communication). Due to the frequency I would rule out most heavy weight protocols (avoid XML, CORBA) as the overhead will probably be too high. If the receiving end needs only access to the latest value, and that access may be less frequent than 0.1 ms, then you don't want to use any protocol that includes queueing as you do not want to read the next element in the queue, you only care about the last, if you did not read the element when it was good, avoid the cost of processing it when it is already stale --i.e. it does not make sense to loop extracting from the queue and discarding.
I would be inclined to use shared memory, or a memory mapped shared file (they are probably quite similar, depends on the platform missing from the question). Depending on the size of the element and the exact hardware architecture (missing from the question) you may be able to avoid locking with a mutex. As an example in current intel processors, read/write access to 32 bit integers from memory is guaranteed to be atomic if the variable is correctly aligned, so in that case you would not be locking.
在我的学校,他们教授 CORBA。他们不应该,它是来自大型机时代的一种古老而可怕的语言,它是委员会设计的经典案例,包含您可能不想要的所有功能,以及您可能想要的一些功能(异步调用?)不是。如果您认为 c++ 规范很大,请再想一想。
不要使用它。
尽管如此,它确实有一个漂亮、易于使用的界面来完成简单的事情。
但不要使用它。
At my school they teach CORBA. They shouldn't, it's an ancient hideous language from the eon of mainframes, it's a classic case of design-by-committee, every feature possible that you don't want is included, and some that you probably do (asynchronous calls?) aren't. If you think the c++ specification is big, think again.
Don't use it.
That said though, it does have a nice, easy-to-use interface for doing simple things.
But don't use it.
它几乎总是通过 C 绑定。
It almost always pass through C binding.