亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python回調(diào)函數(shù)中使用多線程的方法

 更新時(shí)間:2017年12月25日 09:09:51   作者:pirogue  
這篇文章主要介紹了python回調(diào)函數(shù)中使用多線程的方法,需要的朋友可以參考下

下面的demo是根據(jù)需求寫的簡單測試腳本

#!/usr/bin/env python
# coding: utf-8
# 第一個(gè)列表為依賴組件和版本號(hào),后面緊跟負(fù)責(zé)人名稱
# 接著出現(xiàn)第二個(gè)以來組建列表,負(fù)責(zé)人為空了
# 所以根據(jù)需求需要對(duì)組件、版本號(hào)、負(fù)責(zé)人進(jìn)行不同處理
# 這時(shí)在for循環(huán)中根據(jù)if判斷,寫回調(diào)函數(shù)處理
# 格式不一致數(shù)據(jù)的測試數(shù)據(jù)
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):
  # 如果存在負(fù)責(zé)人存在
  try:
    if type(name_value[0]) is unicode:
      # 對(duì)除去列表0個(gè)索引的數(shù)據(jù)(依賴名和版本號(hào))后面的數(shù)據(jù)進(jìn)行遍歷
      for i in name_value:
        # 碰到后面的數(shù)據(jù)是列表的進(jìn)行回調(diào)
        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:
          # 打印依賴、版本號(hào) 負(fù)責(zé)人 開始
          print proj_value+i.split()+['start']
    else:
      # 如果負(fù)責(zé)人后跟的組件這種格式的列表數(shù)據(jù)為空
      # 也就是只有依賴和版本號(hào)列表數(shù)據(jù),負(fù)責(zé)人為空,就打印依賴版本號(hào)
      ver = proj_value
      owner = name_value
      if type(owner[0]) is unicode:
        return git_callback(whole_v, ver, owner)
      else:
        print ver
        # 這里是為了判斷是不是到列表的最后一位
        # 如果是最后一個(gè)值,且不是字符串的Unicode,而是列表
        # 就直接打印出項(xiàng)目
        if whole_v.index(owner[0]) == len(whole_v)-1:
          # 打印最后一個(gè)值
          print whole_v[-1:]
        else:
          # 這里比較繞,打印調(diào)試吧...
          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查詢返回的依賴數(shù)據(jù)格式不唯一的回調(diào)
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

上面的多線程代碼片段是一個(gè)回調(diào)函數(shù),沒有完全根據(jù)demo進(jìn)行改裝,有了demo根據(jù)需求改起來也不難,多調(diào)試就可以了。

python多線程接收多個(gè)參數(shù)

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是你需要多線程調(diào)用的功能函數(shù),list_git是pool_git函數(shù)需要接收的參數(shù),默認(rèn)情況下pool_git是一個(gè)接收一個(gè)參數(shù)的函數(shù)。

但是我們的功能常常設(shè)計(jì)的邏輯比較復(fù)雜,需要在pool_git中傳入多個(gè)參數(shù),這時(shí)list_git就應(yīng)該給一個(gè)多個(gè)元組組成的列表。

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]

Stack Overflow上更多的答疑方便你更好的理解:

https://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments

相信聰明的你一定能看得懂~

多線程與多進(jìn)程

from multiprocessing.dummy import Pool as ThreadPool

多線程進(jìn)程池,綁定一個(gè)CPU核心

from multiprocessing import Pool

多進(jìn)程,運(yùn)行于多個(gè)cpu核心

如果你搞不懂是CPU密集型的任務(wù),還是IO密集型的任務(wù),那就用這個(gè)庫兩條import都寫上,然后分別實(shí)例化跑一下就知道耗時(shí)長短,用法上只是在創(chuàng)建對(duì)象上改幾個(gè)字母就行Pool和ThreadPool的互換。

總結(jié)

以上所述是小編給大家介紹的python回調(diào)函數(shù)中使用多線程的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 使用pandas忽略行列索引,縱向拼接多個(gè)dataframe

    使用pandas忽略行列索引,縱向拼接多個(gè)dataframe

    這篇文章主要介紹了使用pandas忽略行列索引,縱向拼接多個(gè)dataframe的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python入門教程(十七)Python的While循環(huán)

    Python入門教程(十七)Python的While循環(huán)

    這篇文章主要介紹了Python入門教程(十七)Python的While循環(huán),Python是一門非常強(qiáng)大好用的語言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下
    2023-04-04
  • python Pexpect 實(shí)現(xiàn)輸密碼 scp 拷貝的方法

    python Pexpect 實(shí)現(xiàn)輸密碼 scp 拷貝的方法

    今天小編就為大家分享一篇python Pexpect 實(shí)現(xiàn)輸密碼 scp 拷貝的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Matplotlib繪制條形圖的方法你知道嗎

    Matplotlib繪制條形圖的方法你知道嗎

    這篇文章主要為大家詳細(xì)介紹了Matplotlib繪制條形圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助<BR>
    2022-03-03
  • Pandas數(shù)據(jù)分析固定時(shí)間點(diǎn)和時(shí)間差

    Pandas數(shù)據(jù)分析固定時(shí)間點(diǎn)和時(shí)間差

    這篇文章主要介紹了Pandas數(shù)據(jù)分析固定時(shí)間點(diǎn)和時(shí)間差,文章未日澳主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Python 通過截圖匹配原圖中的位置(opencv)實(shí)例

    Python 通過截圖匹配原圖中的位置(opencv)實(shí)例

    今天小編就為大家分享一篇Python 通過截圖匹配原圖中的位置(opencv)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面)

    Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面)

    本文主要介紹了Python函數(shù)調(diào)用的幾種方式(類里面,類之間,類外面),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • scrapy-splash簡單使用詳解

    scrapy-splash簡單使用詳解

    這篇文章主要介紹了scrapy-splash簡單使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 通過自學(xué)python能找到工作嗎

    通過自學(xué)python能找到工作嗎

    在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于通過自學(xué)python能找到工作嗎的分析內(nèi)容,對(duì)此有興趣的朋友們可以參考下。
    2020-06-06
  • Python中FastAPI項(xiàng)目使用 Annotated的參數(shù)設(shè)計(jì)的處理方案

    Python中FastAPI項(xiàng)目使用 Annotated的參數(shù)設(shè)計(jì)的處理方案

    FastAPI 是一個(gè)非常現(xiàn)代化和高效的框架,非常適合用于構(gòu)建高性能的 API,FastAPI 是一個(gè)用于構(gòu)建 API 的現(xiàn)代、快速(高性能)web 框架,基于 Python 類型提示,這篇文章主要介紹了Python中FastAPI項(xiàng)目使用 Annotated的參數(shù)設(shè)計(jì),需要的朋友可以參考下
    2024-08-08

最新評(píng)論