php ice接口调用 demo,PHP通过ice调用python程序

PHP能调用python程序是不是一件很爽的事情,有很多大型的处理简单用PHP是不能完成,我们就可以通过ice中间件调用python来完成。首先创建一个Slice文件Hello.ice

#ifndef HELLO_ICE

#define HELLO_ICE

module Demo

{

interface Hello

{

["cpp:const"] idempotent void sayHello(int delay);

["cpp:const"] idempotent string returnHello();

string test(string name);

void shutdown();

};

};

#endif

有了接口文件,开始创建一个python服务端Server.py,代码如下

import sys, traceback, time, Ice

Ice.loadSlice(“Hello.ice”)

import Demo

class HelloI(Demo.Hello):

def sayHello(self, delay, current=None):

if delay != 0:

time.sleep(delay / 1000.0)

print "Hello World!  aaaaaaaa"

def returnHello(self, current=None):

print current

return “hello”

def test(self, name, current=None):

return name

def shutdown(self, current=None):

current.adapter.getCommunicator().shutdown()

class Server(Ice.Application):

def run(self, args):

if len(args) > 1: #文件编译不能带参数

print self.appName() + ": too many arguments"

return 1

adapter = self.communicator().createObjectAdapter("Hello")

adapter.add(HelloI(), self.communicator().stringToIdentity("hello"))

adapter.activate()

self.communicator().waitForShutdown() #挂起等待

return 0

sys.stdout.flush()

app = Server()

sys.exit(app.main(sys.argv, "config.server"))

如果不知道ice怎么用的孩子可以搜索“ice教程.pdf”,我也还在入门中。下面给出PHP代码,有很多方法,用于属性ice操作,聪明的你肯定不

难。其中值得说明的是oneway相当于异步,twoway相当于同步。如果有返回值就不能使用oneway。最常见的方法是:通过一个oneway开启

处理,再通过一个twoway获取事务状态。

Ice_loadProfile(“hello”);

//

// Change this to true if SSL is configured for the PHP extension.

//

$have_ssl = false;

$res=”";

if(isset($_POST["submitted"]))

{

echo "

";

echo "

Status:

";

try

{

if($have_ssl)

{

$p = $ICE->stringToProxy("hello:tcp -p 10000:udp -p 10000:ssl -p 10001");

}

else

{

$p = $ICE->stringToProxy("hello:tcp -p 60012:udp -p 60012");

}

if(isset($_POST["mode"]))

{

if($_POST["mode"] == "oneway")

{

$p = $p->ice_oneway();

}

elseif($_POST["mode"] == "datagram")

{

$p = $p->ice_datagram();

}

}

$delay = 0;

if(isset($_POST["secure"]) and $_POST["secure"] == "yes")

{

$p = $p->ice_secure(true);

}

if(isset($_POST["timeout"]) and $_POST["timeout"] == "yes")

{

$p = $p->ice_timeout(2000);

}

if(isset($_POST["delay"]) and $_POST["delay"] == "yes")

{

$delay = 2500;

}

if($p->ice_isTwoway())

{

$hello = $p->ice_checkedCast("::Demo::Hello");

}

else

{

$hello = $p->ice_uncheckedCast("::Demo::Hello");

}

if(isset($_POST["sayHello"]))

{

$hello->sayHello($delay);

$res=$hello->test(“this string return from python”); //我Eng很烂

}

elseif(isset($_POST["shutdown"]))

{

$hello->shutdown();

}

echo "OK

";

}

catch(Ice_LocalException $ex)

{

echo "";

print_r($ex);

echo "

";

}

echo "

";

echo "

";

}

?>

=$res;?>

">

Mode:

> Twoway

> Oneway

> Datagram

Options:

> Secure

> Timeout

> Delay

要使整个过程可以运行,把Hello.ice文件移动到php配置的ice的放置目录下,如,在/etc/php.d中查看ice.ini获取ice文件

的存放地址。现在通过终端运行:python Server.py

&(这个&是让程序在后台执行,可以通过jobs命令查看,通过kill % 编号关闭)。到此完了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值