函数等待其他应用程序的响应
我们正在致力于集成两个同时运行并共享数据的不同应用程序。一个应用程序提供数据,另一个应用程序根据外部系统和数据计算一些值,并且必须将其提供回第一个应用程序。
我们使用这个库在应用程序之间共享数据: http:// /grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/Networking
该库基本上允许创建一个可以由任何应用程序查询的共享字典(只要它知道共享字典的位置) )。
因此,应该发生的情况是程序 A 必须向程序 B 提供一些数据,程序 B 使用这些数据并将其他一些数据返回给程序 A。
我的问题是如何让程序 A 等待 B 的响应。更具体地说,我可以将一个对象放入共享字典中,另一个程序收到字典中更改的通知,它可以计算一些属性并更新字典中的对象。程序 A 可以得到通知,但我希望程序 A 等待,直到它返回此响应 - 程序 A 的操作应该基于返回的值。
我认为可以做到这一点的一个非常丑陋的方法是在函数内部有一个无限循环,它不断查询字典以查看对象是否已更新 - 如果它已脱离循环并使用该对象及其计算属性。有谁知道更好的解决方案?
We are working on integrating two different applications that run simultaneously and share data. One application provides the data, the other one computes some values based off external systems and the data and has to provide it back to the first application.
We are using this library to share the data between the applications: http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/Networking
The library basically allows to create a shared dictionary which can be queried by any application (as long as it knows the location of the shared dictionary).
So, what should happen is program A has to provide some data to program B and program B uses this data and returns some other data back to program A.
My problem is how do I make the program A wait for the response from B. In more concrete terms, I can put an object in the shared dictionary, the other program gets notified of a change in the dictionary, it can compute some attribtues and update the object in the dictionary. Program A can get notified, but I want program A to wait till it gets back this response - program A's action should be based on the returned value.
A very ugly way I see this can be done is have an infinite loop inside the function, that keeps querying the dictionary to see if the object has been udpated - if it has break out of the loop and use the object and its computed attributes. Does anyone know of a nicer solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用他们的订阅模型,您可以避免所有无限的-循环。 当您唯一需要检查的时间是实际更新的时间时,为什么您需要循环?因为您订阅了字典中的关键模式,并且当符合该模式的键出现时,您的连接将收到通知更新/添加,您只需要检查即可。
所以基本上,您可以使用 ManualResetEvent 来等待您自己的系统内的同步。以下是 ManualResetEvent 的使用示例:
要转换为您的框架,您可能让订阅处理程序检查更新的内容,找到等待句柄并向其发出信号,从而推进适当的等待线程。
Using their subscription model, you are able to avoid all infinite-looping. Why would you need to loop when the only times you need to check is when something is actually updated? Since you subscribe to key patterns in the dictionary and your connection will be notified when a key fitting that pattern is updated/added, you only need to check on that.
So basically, you can use a ManualResetEvent to wait for synchronization within your own system. Here is an example of usage of ManualResetEvent:
To translate to your framework, you might have the subscription handler check what was updated, find that wait handle and signal it, thus advancing the proper waiting threads.
使用 ManualResetEvent。
use a ManualResetEvent.
如果不使用 IPC 的其他方法(因为您似乎已经选择了这个特定的库),在我看来,连续轮询是唯一的方法。如果您愿意(并且能够)实现其他 IPC 方法,那么您应该停止使用当前的库,而自己在程序之间传递数据。
Without using other methods of IPC (since you've seemed to settle on this particular library) it would seem to me that continuous polling is about the only way. If you are willing (and able) to implement other IPC methods then you should discontinue use of the current library and just pass the data between the programs yourself.
如果我理解正确的话,在程序 B 更新该值之前,程序 A 不得执行任何其他逻辑,并且程序 A 可以从 UCalgary 的该系统获取所述更新的通知。
基于这些假设,我建议在将数据发送到共享字典后,启动一个计时器,其
Tick
事件由一个方法处理,该方法将 (a) 检查字典是否有更新,或者(b) 检查由来自共享字典库的通知设置的变量。一旦发现更新,停止计时器,并执行将开始处理新更新的值的方法。
计时器与您建议的“无限循环”没有什么不同,但它实际上没有处理开销,可以设置为以实际间隔(即 1 秒、1 分钟、24 小时)进行检查,并且可以设置为时间如果程序 B 或字典的事情没有按计划进行,则退出。
If I understand correctly, Program A mustn't execute any additional logic until Program B has updated the value, and Program A can get a notification of said update from that system out of UCalgary.
Based on those assumptions, I'd suggest that after sending data to the shared dictionary, you start a timer whose
Tick
event is handled by a method that will either (a) check the dictionary for updates, or (b) check a variable that gets set by the notification that comes in from the shared dictionary library.Once the update has been found, stop the timer, and execute the method that will pick up processing the newly updated value.
The timer is not unlike the "infinite loop" you suggested, but it has virtually no processing overhead, can be set to check at a realistic interval (be that 1 second, 1 minute, 24 hours), and can be set up to time out if things don't go as planned with Program B or the dictionary.