最近在调试一个算法,想通过改变算法的参数看看结果有什么变化。 碰到一个麻烦的事情是,从磁盘加载、构建数据需要15分钟。这就比较讨厌了,也就是说我每次调一个参数前都要等15分钟启动时间?

于是我就想,能不能开一个datahost进程专门加载数据。我在别的进程调试参数,但需要用数据时就直接从datahost进程中加载现成的数据。 这样的话我只需要从磁盘加载一次数据。 于是找了一下, c#有个叫IPC(inter process communication)的功能可以实现同一台机器上的不同进程间通信。

注意我这里的scenario:我需要进程间通信“数据对象”, 而不是消息。 通信消息比较好办,就传一个string流就行了。而我需要传递的是数据对象,比如一个Dictionary, 或者自定义class。

IPC的大致过程是这样的:datahost进程先绑定一个对象,client根据对象的uri去访问对象类, 并且调用该类的方法。datahost进程接收到从client发来的调用,就会实例化一个对象(注意,在这个时候才开始实例化一个对象,这也是我的代码里使用static的原因),并且执行相关的函数,再把结果返回给client。 注意执行过程是在datahost端的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IPCServer
{
public class DataObj : MarshalByRefObject
{
public static Dictionary<string, int> salary;
public static string company;
public static int counter; public static int constructerCnt = ; public static void LoadData()
{
company = "Microsoft";
salary = new Dictionary<string, int>();
salary.Add("lianjx", );
salary.Add("uncle", );
counter = ;
} public Dictionary<string, int> GetSalary()
{
return DataObj.salary;
} public DataObj()
{
DataObj.constructerCnt++;
Console.WriteLine("Constructer...{0}", DataObj.constructerCnt);
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Text;
using System.Threading.Tasks; namespace IPCServer
{
class Program
{
static void Main(string[] args)
{
IpcServerChannel channel = new IpcServerChannel("ServerChannel");
ChannelServices.RegisterChannel(channel, false);
DataObj.LoadData();
DataObj.salary.Add("jian", );
DataObj.salary.Add("xun", );
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DataObj), "DataObj", WellKnownObjectMode.SingleCall); Console.WriteLine("press anykey to exit");
Console.ReadKey();
}
}
}
using IPCServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Text;
using System.Threading.Tasks; namespace IPCClient
{
class Program
{
static void Main(string[] args)
{
IpcClientChannel channel = new IpcClientChannel();
ChannelServices.RegisterChannel(channel, false); DataObj obj = null; for (int t = ; t < ; t++)
{
Console.WriteLine("t={0}", t);
try
{
obj = (DataObj)Activator.GetObject(typeof(DataObj), "ipc://ServerChannel/DataObj");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
} if (obj == null)
{
Console.WriteLine("could not locate server");
}
else
{
foreach (var pair in obj.GetSalary())
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
Console.WriteLine("counter = {0}", obj.GetSalary());
DataObj.counter++;
}
} Console.WriteLine("Mission Complete!");
Console.ReadKey();
}
}
}

事实上结果并没有加速。估计IPC也是把对象序列化后传递,再反序列化的吧。 桑心。

next steps: 寻找一种直接读其他进程的内存空间的途径。。。

转至:http://www.cnblogs.com/sylvanas2012/p/4294393.html

c# 进程间通信 IPC的更多相关文章

  1. Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)

    Linux进程间通信IPC学习笔记之同步二(SVR4 信号量)

  2. Linux进程间通信IPC学习笔记之同步二(Posix 信号量)

    Linux进程间通信IPC学习笔记之同步二(Posix 信号量)

  3. Linux进程间通信IPC学习笔记之消息队列(SVR4)

    Linux进程间通信IPC学习笔记之消息队列(SVR4)

  4. Android进程间通信IPC

    一.IPC的说明 IPC是Inter-Process Communication的缩写,含义为进程间通信或跨进程通信,是指两个进程之间进行数据交换的过程. IPC不是Android独有的,任何一个操作 ...

  5. 进程间通信IPC -- 管道, 队列

    进程间通信--IPC(Inter-Process Communication) 管道 from multiprocessing import Pipecon1,con2 = Pipe()管道是不安全的 ...

  6. [原创]chromium源码阅读-进程间通信IPC.消息的接收与应答

    chromium源码阅读-进程间通信IPC.消息的接收与应答   chromium源码阅读-进程间通信IPC.消息的接收与应答 介绍 chromium进程间通信在win32下是通过命名管道的方式实现的 ...

  7. 进程间通信IPC之--无名管道(pipe)和有名管道(fifo)(转)

     进程间通信IPC之--无名管道(pipe)和有名管道(fifo) 2012-01-17 22:41:20 分类: C/C++ 每个进程各自有不同的用户地址空间,任何一个进 程的全局变量在另一个进程中 ...

  8. 进程间通信IPC、LPC、RPC

    进程间通信(IPC,Inter-Process Communication),指至少两个进程或线程间传送数据或信号的一些技术或方法.进程是计算机系统分配资源的最小单位.每个进程都有自己的一部分独立的系 ...

  9. 【Android】进程间通信IPC——Binder

    Binder是Android中的跨进程通信方式,bindService的时候,服务端返回Binder对象,通过该对象客户端可以从服务端获取数据.在进程间通信IPC——AIDL中创建了ICustomAi ...

  10. 【Android】进程间通信IPC——AIDL

    AIDL官网定义AIDL(Android 接口定义语言)与您可能使用过的其他 IDL 类似. 您可以利用它定义客户端与服务使用进程间通信 (IPC) 进行相互通信时都认可的编程接口. 在 Androi ...

随机推荐

  1. C++中的静态绑定和动态绑定

    C++在面向对象编程中,存在着静态绑定和动态绑定的定义,本节即是主要讲述这两点区分.我是在一个类的继承体系中分析的,因此下面所说的对象一般就是指一个类的实例.首先我们需要明确几个名词定义: 静态类型: ...

  2. SQL Server技术问题之自定义函数优缺点

    优点: 可以在SQL语句中调用,直接使用返回值,从而可以形成复杂的SQL应用. 缺点: 能在函数中使用的语句有严格限制: 不支持create.ALTER.drop等DDL(Data Definitio ...

  3. 在VS中使用TinyFox调试OWIN应用(转)

    在很早一段时间之前,我曾经写过一篇关于Katana的使用方法的文章<如何安装并简单的使用OwinHost——Katana>,上面就有介绍如何在VS中调试使用Katana作为Host的App ...

  4. Node.js爬虫数据抓取乱码问题总结

    一.非UTF-8页面处理 1.背景 windows-1251编码 比如俄语网站:https://vk.com/cciinniikk 可耻地发现是这种编码 所有这里主要说的是 Windows-1251( ...

  5. SQLServer根据不同前缀生成多套流水号

    --种子表 --@prefix 前缀 --@seed 种子值 create table RefNoSeed( prefix ) unique, seed int ) go --测试表 --@inser ...

  6. VC Windows API获得桌面所有窗口句柄的方法

    VC Windows API应用之GetDesktopWindow ——获得桌面所有窗口句柄的方法 Windows API Windows 这个多作业系统除了协调应用程序的执行.分配内存.管理资源…之 ...

  7. Try out the latest C++ compiler toolset without waiting for the next update of Visual Studio

    Updated 22/Apr/2016: The NuGet package is now being uploaded daily. The payload doesn’t change every ...

  8. ActiveReports 9 新功能:可视化查询设计器(VQD)介绍

    在最新发布的ActiveReports 9报表控件中添加了多项新功能,以帮助你在更短的时间里创建外观绚丽.功能强大的报表系统,本文将重点介绍可视化数据查询设计器,无需手动编写任何SQL语句,主要内容如 ...

  9. EntityFramework 更新表结构到数据库

    在程序包管理器控制台 1.执行:Enable-Migrations -Force  生成:Migrations 2 修改AutomaticMigrationsEnabled默认为false改为true ...

  10. USE “schema_name” in PostgreSQL

    http://timmurphy.org/tag/mysql/ http://timmurphy.org/2009/11/17/use-schema_name-in-postgresql/ ===== ...