日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python3 Gearman 使用

發布時間:2024/3/26 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python3 Gearman 使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

網上很多資料對python gearman的使用都是用的gearman模塊,然而這個模塊僅支持python2.x ,使用python3的pip install安裝,會因為不支持的python2語法報錯,根本無法使用。 想用python3的資料特別少。

安裝

首先先安裝gearman:sudo apt-get install gearman libgearman-dev

安裝python3對應的gearman模塊
安裝:pip install python3-gearman
如果系統里有多個版本的python,注意使用python3 -m pip install python3-gearman 避免安裝到別的版本里去。

最簡示例:

server端:

import python3_gearman gm_worker = python3_gearman.GearmanWorker(['localhost:4730'])# 定義收到消息的處理函數,返回值必須是個byte string。 def task_listener(gearman_worker, gearman_job):print(gearman_job.data)return "" # 注冊消息處理,msg為自定義的,和client保持一致 gm_worker.register_task('msg', task_listener) # 啟動監聽 gm_worker.work()

client端:

import python3_gearman gm_client = python3_gearman.GearmanClient(['localhost:4730', 'otherhost:4730']) gm_client.submit_job("msg", "hello world") # 發送消息,msg是自定義的,要和server端協商保持一致

先啟動server,然后運行client。成功的話,client非阻塞,每次運行,server端會打印出對應的字符串。

進階實例:

  • 兩端發送和接收解析相對復雜的json數據:# server端:將上面的tesk_listener函數改為如下即可: j = json.loads(gearman_job.data) # client端:用json.dumps將一個復雜的字典傳入,代替原來的"hello world即可"
  • 客戶端對發送結果進行校驗
    submit_job的返回值是一個結果定值,可以通過校驗這個值來判斷發送是否成功:import python3_gearman from python3_gearman.job import JOB_UNKNOWN def check_request_status(job_request):if job_request.complete:print ("Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result))elif job_request.timed_out:print ("Job %s timed out!" % job_request.unique)elif job_request.state == JOB_UNKNOWN:print ("Job %s connection failed!" % job_request.unique)gm_client = python3_gearman.GearmanClient(['localhost:4730', 'otherhost:4730'])submitted_job_request = gm_client.submit_job("msg", "hello world")check_request_status(submitted_job_request)
  • 設置數據高優先級,設置發送方式為后臺非阻塞模式
    可以通過submit_job的參數進行配置,priority=python3_gearman.PRIORITY_HIGH指定高優先級, background=True指定通過后臺非阻塞的方式發送。submitted_job_request = gm_client.submit_job("msg", "hello world", priority=python3_gearman.PRIORITY_HIGH, background=True)

完整代碼:
server端:

import python3_gearman import jsongm_worker = python3_gearman.GearmanWorker(['localhost:4730'])# 定義收到消息的處理函數,返回值必須是個byte string。 def task_listener(gearman_worker, gearman_job):print(gearman_job.data)j = json.loads(gearman_job.data)print(json.dumps(j,sort_keys=True,indent=4, ensure_ascii=False,separators=(',',':'))) # 格式化輸出json字符串return "" # 注冊消息處理,msg為自定義的,和client保持一致 gm_worker.register_task('msg', task_listener) # 啟動監聽 gm_worker.work()

client端:

import python3_gearman from python3_gearman.job import JOB_UNKNOWN import jsondef check_request_status(job_request):if job_request.complete:print ("Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result))elif job_request.timed_out:print ("Job %s timed out!" % job_request.unique)elif job_request.state == JOB_UNKNOWN:print ("Job %s connection failed!" % job_request.unique)gm_client = python3_gearman.GearmanClient(['localhost:4730', 'otherhost:4730'])msg_dict = {"name":"threedog","age":18,"class":{"name":"一班","number":1} }submitted_job_request = gm_client.submit_job("msg", json.dumps(msg_dict), priority=python3_gearman.PRIORITY_HIGH, background=False)check_request_status(submitted_job_request)

文檔

對于任何框架和工具,文檔都是非常重要的一部分。比如上面的例子中,我們想知道除了JOB_UNKNOWN還有什么其他的狀態?除了PRIORITY_HIGH還有什么可選的優先級。這些都需要借助文檔。

然而python3-gearman這個小項目網上資料很少,而且沒有找到官網文檔的地址。

好在,潛心研究之下還是找到了官方提供的文檔所在:這個項目是托管在github上的:https://github.com/josiahmwalton/python3-gearman 點開這個項目你會發現有自帶的docs

只不過這個里面放的并不是html或者md格式的文檔,而是一些rst后綴的文本文件和一個Makefile。這是一種很常用的文檔生成的格式,rst就是文檔源格式,便于書寫,但是可讀性較差。可以通過在這個目錄下運行執行make +格式的方式,生成指定格式的文檔。

操作方法如下:

# 安裝工具 sudo apt-get install python3-sphinx # 文檔下載 git clone https://github.com/josiahmwalton/python3-gearman.git # 文檔生成 cd python3-gearman/docs/ make html # 指定為html格式,直接輸入make會提示有哪些可選的格式

完成后會在docs目錄下生成一個_build目錄,里面包含詳細的官方文檔和實例,我上面的示例代碼也是從官方文檔的實例中抽絲剝繭扒出來的:

總結

以上是生活随笔為你收集整理的Python3 Gearman 使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。