关闭

python 和 php 通信3

标签: pythonphpattributesjsonfunctionimport
1434人阅读 评论(0) 收藏 举报
分类:

gearman

python easy_install .****.zip 即可安装python-gearman

用gearman 通信最重要的是  发送数据的时候要用json包装一下然后再发送

import gearman,sys,json
from lxml.html.soupparser import fromstring
gm_worker = gearman.GearmanWorker(['127.0.0.1:4730'])


# See gearman/job.py to see attributes on the GearmanJob
# Send back a reversed version of the 'data' string
def task_listener_xpath(gearman_worker, gearman_job):
#print gearman_job.data
##return json.dumps('data')
doc = json.loads(gearman_job.data)
root = fromstring(doc["content"])
find_text = root.xpath(doc["xpath"])
text = find_text[0].text
print "Title is : " + find_text[0].text
return json.dumps(text)




# gm_worker.set_client_id is optional
#gm_worker.set_client_id('your_worker_client_id_name')
gm_worker.register_task('xpath', task_listener_xpath)


# Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
gm_worker.work()、


php客户端


<?php
set_time_limit(0);
$client= new GearmanClient();  
$client->addServer("127.0.0.1", 4730);  
    #$client= Gearman::getClient();
    $funcName = 'xpath';
    #$url = "http://www.baidu.com/";
    $url = "http://www.youdao.com/";
    $encoding = "utf-8";
    $content = file_get_contents($url);
    $xpath ='//html//head//title';  
   # $data = $content;
#$data = "test";
$data = json_encode($content);
$data = array("content"=>$content, "xpath"=>$xpath);
    $data = json_encode($data);
$result =  $client->do($funcName, $data);  
    //  $data = array('content'=>$content,'xpath'=> $xpath);        
         #$result = Gearman::work($funcName, $data);  
         #$result = Gearman::work($funcName, json_encode($data));    
    var_dump($result);      
var_dump(json_decode($result));
 ?>


python 客户端

import gearman,json
gm_client = gearman.GearmanClient(['127.0.0.1:4730'])
# See gearman/job.py to see attributes on the GearmanJobRequest
# Defaults to PRIORITY_NONE, background=False (synchronous task), wait_until_complete=True
a = json.dumps(["efg","abc"])
completed_job_request = gm_client.submit_job("xpath", a)
#check_request_status(completed_job_request)
print completed_job_request.result



php服务端


<?php
$worker= new GearmanWorker();
$worker->addServer("127.0.0.1", 4730);
$worker->addFunction("xpath", "title_function");
while ($worker->work());


function title_function($job)
{
$str = $job->workload();
return strlen($str);
}
?>








0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:137847次
    • 积分:3474
    • 等级:
    • 排名:第9965名
    • 原创:214篇
    • 转载:22篇
    • 译文:0篇
    • 评论:9条
    博客专栏