如何构建一个由小程序相互提供数据的迷你网络?

发布于 2024-08-06 03:54:56 字数 96 浏览 3 评论 0 原文

我正在尝试模拟一个实时网络,其中节点是不同速率的消费者和生产者。我如何使用 Python 快速实现这个示例?我想象我会为每个节点编写一个简单的程序,但我不确定如何将它们相互连接。

I'm trying to simulate a real-time network where nodes are consumers and producers of different rates. How would I quickly implement a sample of this with Python? I was imagining that I'd write a simple program for each node, but I'm not sure how to connect them to each other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

网白 2024-08-13 03:54:56

至少一开始就坚持传统的模拟结构

您的目标是编写一个异步系统作为练习吗?如果是这样,那么我想你至少必须实现一个多线程(如果不是多进程或网络系统)。

但是,如果它确实是一个模拟,并且您想要的是分析结果,那么实现实际的分布式模型将是一种非常复杂的方法,它产生的数据可能比抽象模拟(即模拟本身)少得多。 不必异步参与者通信的网络。这只是让问题变得如此困难而无法解决的好方法。

我说,坚持传统的模拟架构。

经典离散事件模拟

其工作方式是,作为中央数据结构,您拥有待处理事件的排序集合。事件自然地按时间增加排序。

该程序有一个主循环,它从集合中取出下一个(即,最低值的)事件,将模拟时钟前进到该事件的时间,然后调用与该事件关联的任何特定于任务的处理。

但是,您会问,如果在模拟器刚刚跳过的时间增量内应该发生一些事情怎么办?嗯,根据定义,什么也没有。如果模拟的单个元素需要在该时间间隔内发生某些事情,则它负责分配事件并将其插入(已排序的)集合中。

虽然有许多适合模拟的软件包和程序,但模拟的本质并不难,并且用您最喜欢的语言从头开始编写它是完全合理的。

玩得开心!

Stick with traditional simulation structures, at least at first

Is it your goal to write an asynchronous system as an exercise? If so, then I guess you have to implement at least a multi-threaded if not multi-process or network system.

But if it's really a simulation, and what you want are the analysis results, implementing an actual distributed model would be a horribly complex approach that is likely to yield far less data than an abstract simulation would, that is, the simulation itself doesn't have to be a network of asynchronous actors communicating. That would just be a good way to make the problem so hard it won't get solved.

I say, stick with the traditional simulation architecture.

The Classic Discrete Event Simulation

The way this works is that as a central data structure you have a sorted collection of pending events. The events are naturally sorted by increasing time.

The program has a main loop, where it takes the next (i.e., the lowest-valued) event out of the collection, advances the simulation clock to the time of this event, and then calls any task-specific processing associated with the event.

But, you ask, what if something was supposed to happen in the time delta that the simulator just jumped across? Well, by definition, there was nothing. If an individual element of the simulation needed something to happen in that interval, it was responsible for allocating an event and inserting it into the (sorted) collection.

While there are many packages and programs out there that are geared to simulation, the guts of a simulation are not that hard, and it's perfectly reasonable to write it up from scratch in your favorite language.

Have fun!

故笙诉离歌 2024-08-13 03:54:56

进程间通信通常很难正确完成。您可能需要考虑另一种方法是否可以满足您的需求,例如离散事件模拟器。

在 DES 中,您实际上并不执行每个节点的工作,只需模拟每个节点完成工作所需的时间。您可以使用优先级队列来跟踪传入和传出的工作,并检测系统以跟踪全局和每个节点的队列大小。

也许如果您提供有关您想要完成的任务的更多详细信息,我们可以提供更具体的建议。

编辑:Python在heapq模块中有一个内置的优先级队列,参见http://docs.python.org/library/heapq.html

Inter-process communication is a generally hard thing to get right. You may want to consider if another approach would meet your needs, like a discrete events simulator.

In a DES, you don't actually do the work of each node, just simulate how long each node would take to get the work done. You can use a priority queue to keep track of the incoming and outgoing work, and instrument the system to keep track of the queue size globally, and for each node.

Perhaps if you provide more detail on what you're trying to accomplish, we can give more specific recommendations.

Edit: Python has a built-in priority queue in the heapq module, see http://docs.python.org/library/heapq.html.

离旧人 2024-08-13 03:54:56

我喜欢 @DigitalRoss 和 dcrosta 对离散偶数模拟的建议,我想指出 sched 模块正是您在此类系统的核心所需要的(无需在 heapq 之上重建核心,或以其他方式)。您只需要初始化一个 sched.scheduler 实例,而不是通常的 time.timetime.sleep,方法是向它传递两个可调用对象模拟时间的流逝

例如:

class FakeTime(object):
  def __init__(self, start=0.0):
    self.now = start
  def time(self):
    return self.now
  def sleep(self, delay):
    self.now += delay

mytimer = FakeTime()

并使用 s = sched.scheduler(mytimer.time, mytimer.sleep) 实例化调度程序。

I like @DigitalRoss's and dcrosta's suggestions of a discrete-even simulation, and I'd like to point out that the sched module in the Python standard library is just what you need at the core of such a system (no need to rebuild the core on top of heapq, or otherwise). You just need to initialize a sched.scheduler instance, instead of the usual time.time and time.sleep, by passing it two callables that simulate the passage of time.

For example:

class FakeTime(object):
  def __init__(self, start=0.0):
    self.now = start
  def time(self):
    return self.now
  def sleep(self, delay):
    self.now += delay

mytimer = FakeTime()

and use s = sched.scheduler(mytimer.time, mytimer.sleep) to instantiate the scheduler.

遗忘曾经 2024-08-13 03:54:56

查看 NetworkX 用于创建和操作网络的 Python 库。编辑:注意,与 NetworkX 相邻/相关,并且也在洛斯阿拉莫斯 NL 托管的是 PyGraphviz,一个显示图形的实用程序。感谢 las3jrock 指出我最初的链接错误。

您可以按原样使用 NetworkX,或者从这个库中获得灵感(我不会打扰这个库确实有网络模拟所需的“所有事物图”。)在任何情况下,这种类型的图创建和操纵将允许您代表(并增长/缩小/进化)网络。

使用基于上述图形库的单个集中式对象/服务,您将需要创建一个(或多个)类来表示网络节点的行为。

然后,根据您的需求,如果网络相对较小,网络节点可以有效地“释放”,并在线程内部运行。或者(这通常很容易管理模拟),可以有集中式方法,它迭代节点并在适当时调用它们的“do_it”方法。如果这种集中式控制器的“刷新率”足够高,则可以使用节点级别的实时时钟来确定何时应触发节点的特定行为。这种模拟可以是事件驱动的,也可以是简单的轮询(同样,如果刷新周期相对于时钟的基本时间单位足够低)。

网络的集中“地图”将提供各种与网络相关的原语

或者,网络结构可以分布在网络本身的节点上,就像互联网一样。这种分散的方法有几个优点,但它意味着向节点添加逻辑和数据,以便它们实现相当于 DNS 和路由的功能。这种方法还有一个缺点,即要求节点之间的大量流量与网络拓扑的发现和维护相关,而不是与网络旨在实现的任何通信/交换语义相关。仿真。简而言之,我不建议使用这种分散的方法,除非当前的模拟旨在研究此类分布式网络管理系统的协议。

编辑:

几个回复中建议的 DES 方法确实解决了问题的模拟部分。如果问题的网络部分很重要,那么实现基于强大图形库的虚拟网络将是解决方案的重要组成部分。这种方法将更容易地揭示与网络拓扑相关的系统动态。

Look into NetworkX a Python library for creating and manipulating networks. Edit: Attention, nearby/related to NetworkX, and also hosted at Los Alamos NL, is PyGraphviz, a utility to display graphs. Thank you to las3jrock to point out that I had the wrong link initialy.

You can either use NetworkX as-is, or get inspiration from this library (I wouldn't bother this library really has "all things graph" that seem to be needed for network simulation.) In any case, this type of graph creation and manipulation would allow you to represent (and grow / shrink / evolve) the network.

With a single centralized object/service based on a graph library as mentioned, you'd be left with creating one (or several) class to represent the behavior of the network nodes.

Then, depending on your needs, and if the network is relatively small, the network nodes could effectively be "let loose", and run inside of threads. Alternatively (and this is often easy to manage for simulations), there could be a centralized method which iterates through the nodes and invoke their "do_it" methods when appropriate. If the "refresh rate" of such a centralized controller is high enough, real-time clocks at the level of nodes can be used to determine when particular behaviors of the node should be triggered. This simiulation can be event driven, or simply polled (again if refresh period is low enough relative to the clocks' basic unit of time.)

The centralized "map" of the network would offer the various network-related primitives required by the network nodes to perform their "job" (whatever this may be). In other word, the nodes could inquire from the "map" the list of their direct neighbors, the directory of the complete network, the routes (and their cost) towards any given node etc, .

Alternatively, the structure of the network could be distributed over the nodes of the network itself, à la Internet. This decentralized approach has several advantages, but it implies adding logic and data to the nodes so they implement the equivalent of DNS and Routing. This approach also has the drawback of requiring that a fair amount of traffic between nodes be related to the discovery and maintenance of the topology of the network rather than to whatever semantics of communication/exchange the network is meant to emulate. In a nutshell, I wouldn't suggest using this decentralized approach, unless the simulation at hand was aimed at studying the very protocols of such distributed network management systems.

Edit:

The DES approach suggested in several replies certainly addresses the simulation part of the question. If the network part of question is important, implementing a virtual network based on a strong graph library will be an important part of the solution. Such an approach would expose more readily the dynamics of the system associated with the network topology.

も让我眼熟你 2024-08-13 03:54:56

以下是制作基本客户端/服务器程序的方法:
http://wdvl.internet.com/Authoring/python/client/watts06152009。 html

对于从一个到另一个发送的数据,我建议使用 JSON,这是最简单的格式曾经。
如果你想在 python 上实现,请检查 simplejson:
http://pypi.python.org/pypi/simplejson/

Here's how to make a basic client/server program:
http://wdvl.internet.com/Authoring/python/client/watts06152009.html

For the data to send from one to another, I'd recommend JSON, the most simple format ever.
Check simplejson if you want to implement it on python:
http://pypi.python.org/pypi/simplejson/

青春有你 2024-08-13 03:54:56

我想到两件事:

1- 您可以使用 Twisted Python 编写一个或多个守护进程。 (请注意,Twisted 可能会有点让人不知所措,因为它是一个事件驱动的异步系统)。每个守护进程都可以绑定到一个端口,并使自己可供其他守护进程使用。或者,您可以只在一个守护进程中运行所有内容,并让您编写的每个“进程”以不同的时间间隔触发......并通过绑定端口相互通信。

2-您可以使用单个事件驱动的核心(有几个),并且只需为每个任务分叉一堆进程或线程。

Two things come to mind:

1- You could write one or more daemons with Twisted Python. (Be warned, twisted can get a little overwhelming , since its an event-driven async system ). Each daemon can bind to a port and make itself available to other daemons. Alternately, you could just run everything within one daemon, and just have each "process" that you script fire at a different interval.. and talk to one another through bound ports as well.

2- You could use a single event driven core -- there are a few --, and just fork a bunch of processes or threads for each task.

2024-08-13 03:54:56

只需使用 Stackless Python,创建 tasklet,将它们与通道连接起来,一切都会正常进行。这非常简单。

Just use Stackless Python, create tasklets, connect them with channels, and everything will work. It is extremely simple.

囚你心 2024-08-13 03:54:56

这是 Stackless 做得非常好的事情。

另外,您可以使用生成器/协同例程。

有趣的链接:

http://www.python.org/dev/peps/pep- 0342/

新用户只能发布1个超链接...所以这是另一个

'/'.join(['http:/', 'us.pycon.org', '2009', 'tutorials', 'schedule', '1PM6/'])

This is something Stackless does very well.

Also, you could use generators / co-routines.

Interesting links:

http://www.python.org/dev/peps/pep-0342/

New users can only post 1 hyper link... so here is the other one

'/'.join(['http:/', 'us.pycon.org', '2009', 'tutorials', 'schedule', '1PM6/'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文