python回調函數中使用多線程的方法

這篇文章主要介紹了python回調函數中使用多線程的方法,需要的朋友可以參考下
下面的demo是根據需求寫的簡單測試腳本

#!/usr/bin/env python
# coding: utf-8
# 第一個列表爲依賴組件和版本號,後面緊跟負責人名稱
# 接着出現第二個以來組建列表,負責人爲空了
# 所以根據需求需要對組件、版本號、負責人進行不同處理
# 這時在for循環中根據if判斷,寫回調函數處理
# 格式不一致數據的測試數據
a = [[u'tool-1', u'1.9.13'], u'xiaowang', u'xiaoqu', [u'tool-2', u'1.9.23'], [u'tool-3', u'1.9.33'], [u'tool-4', u'1.9.43'], u'pi',[u'tool-5', u'1.9.53']]
# a = [[u'tool-1', u'1.9.13'],u'xiaowang',[u'tool-2', u'1.9.23'],u'xiaowang', [u'tool-3', u'1.9.33'],u'xiaowang']
# a = [[u'tool-1', u'1.9.13']]
# [u'tool-1', u'1.9.13']
your_pro = a[0]
# print your_pro
# [u'xiaowang', u'xiaoqu', [u'tool-2', u'1.9.23']]
tmp = a[1:]
# print tmp
def git_callback(whole_v, proj_value, name_value):
  # 如果存在負責人存在
  try:
    if type(name_value[0]) is unicode:
      # 對除去列表0個索引的數據(依賴名和版本號)後面的數據進行遍歷
      for i in name_value:
        # 碰到後面的數據是列表的進行回調
        if type(i) is list:
          tmp_index = whole_v.index(i)+1
          return git_callback(whole_v, whole_v[whole_v.index(i)], whole_v[tmp_index:])
        else:
          # 打印依賴、版本號 負責人 開始
          print proj_value+i.split()+['start']
    else:
      # 如果負責人後跟的組件這種格式的列表數據爲空
      # 也就是隻有依賴和版本號列表數據,負責人爲空,就打印依賴版本號
      ver = proj_value
      owner = name_value
      if type(owner[0]) is unicode:
        return git_callback(whole_v, ver, owner)
      else:
        print ver
        # 這裏是爲了判斷是不是到列表的最後一位
        # 如果是最後一個值,且不是字符串的Unicode,而是列表
        # 就直接打印出項目
        if whole_v.index(owner[0]) == len(whole_v)-1:
          # 打印最後一個值
          print whole_v[-1:]
        else:
          # 這裏比較繞,打印調試吧...
          new_ver = whole_v[whole_v.index(ver)+1]
          owner = whole_v[whole_v.index(ver)+2:]
          return git_callback(whole_v, new_ver, owner)
  except IndexError as e:
    print proj_value
    print e
git_callback(a, your_pro, tmp)

demo的output:Boom:

git_response pirogue$ python test.py
[u'tool-1', u'1.9.13', u'xiaowang', 'start']
[u'tool-1', u'1.9.13', u'xiaoqu', 'start']
[u'tool-2', u'1.9.23']
[u'tool-3', u'1.9.33']
[u'tool-4', u'1.9.43', u'pi', 'start']
[u'tool-5', u'1.9.53']
list index out of range

python的多線程

下面的代碼是從主程序中,摘取出來的代碼片段

from multiprocessing.dummy import Pool as ThreadPool
# 判斷git查詢返回的依賴數據格式不唯一的回調
def git_callback(whole_v, proj_value, name_value, git_cookie):
  # 
  whole_v = whole_v
  list_git = []
  if name_value:
    # print name_value
    for i in name_value:
      # print i
      if i:
        if type(i) is list:
          tmp_index = whole_v.index(i)+1
          return git_callback(whole_v, whole_v[whole_v.index(i)], whole_v[tmp_index:], git_cookie)
        else:
          git_cookie = str(git_cookie.split()[0])+' '+str(git_cookie.split()[1])
          list_git.append(tuple(git_cookie.split("?")+i.split()))
          print list_git
          pool = ThreadPool(100)
          result = pool.map(pool_git, list_git)
          print result
          pool.close()
          pool.join()          
  else:
    print proj_value

上面的多線程代碼片段是一個回調函數,沒有完全根據demo進行改裝,有了demo根據需求改起來也不難,多調試就可以了。

python多線程接收多個參數

from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(100)
result = pool.map(pool_git, list_git)
print result
pool.close()
pool.join()

pool_git是你需要多線程調用的功能函數,list_git是pool_git函數需要接收的參數,默認情況下pool_git是一個接收一個參數的函數。

但是我們的功能常常設計的邏輯比較複雜,需要在pool_git中傳入多個參數,這時list_git就應該給一個多個元組組成的列表。

stackoverflow上老外給的代碼示例:

def multi_run_wrapper(args):
  return add(*args)
def add(x,y):
  return x+y
if __name__ == "__main__":
  from multiprocessing import Pool
  pool = Pool(4)
  results = pool.map(multi_run_wrapper,[(1,2),(2,3),(3,4)])
  print results
output
[3, 5, 7]

多線程與多進程

from multiprocessing.dummy import Pool as ThreadPool

多線程進程池,綁定一個CPU核心

from multiprocessing import Pool

多進程,運行於多個cpu核心

如果你搞不懂是CPU密集型的任務,還是IO密集型的任務,那就用這個庫兩條import都寫上,然後分別實例化跑一下就知道耗時長短,用法上只是在創建對象上改幾個字母就行Pool和ThreadPool的互換。
寫到這裏,給大家推薦一個資源很全的python學習聚集地,點擊進入,這裏有資深程序員分享以前學習

心得,學習筆記,還有一線企業的工作經驗,且給大家精心整理一份python零基礎到項目實戰的資料,

每天給大家講解python最新的技術,前景,學習需要留言的小細節

總結

以上所述是小編給大家介紹的python回調函數中使用多線程的方法,希望對大家有所幫助

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章