为什么在Python中使用池多处理会有不同的结果?

时间:2019-11-10 02:18:26

标签: python multiprocessing

我有此函数,该函数返回具有命名列表元素的字典,如下所示:

lista =['GILDIKNK','MENIA']
dic={}

def nome_arquivos(lista_fragmentos):
    for elemento in lista_fragmentos:
        indice = "fragmento_" + str(lista_fragmentos.index(elemento)+1)
        dic[indice] = elemento
    print(dic)

nome_arquivos(lista)

在这种情况下,输出是(也是我想要的):

{'fragmento_1': 'GILDIKNK', 'fragmento_2': 'MENIA'}

但是,我正在尝试插入多处理程序,以解决更大的问题。这是我的代码:

from multiprocessing.pool import ThreadPool
import time

lista =['GILDIKNK','MENIA']
dic={}

def nome_arquivos(lista_fragmentos):
    for elemento in lista_fragmentos:
        indice = "fragmento_" + str(lista_fragmentos.index(elemento)+1)
        dic[indice] = elemento
    print(dic)

def pool_handler():
    pool = ThreadPool(4)
    results = pool.map(nome_arquivos, lista)


if __name__ == '__main__':
    pool_handler()

输出为:

{'fragmento_1': 'G', 'fragmento_2': 'I', 'fragmento_3': 'L', 'fragmento_4': 'D',
'fragmento_6': 'K', 'fragmento_7': 'N'}{'fragmento_1': 'M', 'fragmento_2': 'E',
'fragmento_3': 'N', 'fragmento_4': 'I', 'fragmento_6': 'K', 'fragmento_7': 'N',
'fragmento_5': 'A'}

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

对于为什么您的代码不起作用,已经有一些很好的评论,所以我想我会尝试重写您的第一个函数(没有任何多处理的函数)。它本身应该足够有效,如果有必要进行多处理,我会感到惊讶。

import UIKit
import SQLite

struct Data_On_Case
{
    var dataID: Int64
    var dataName: String
}

struct MyData
{
    let dataID: Int64
    var dataName: String
    var dataType: String?
    var dataDescription: String?
    var pricePaid: Double?
    var shipping: Double?
    var datePurchased: String?
    var dataDuration: String?
    var manufacturer: String?
    var venue: String?
    var performanceLevel: String?
    var dataNotes: String?
    var dataStyle: String?
}

class Test_VC: UIViewController
{
    private let crossRefTable = Table("CrossRefCase_Data")
    private let crossRefRCaseID = Expression<Int64>("CaseID_CaseXRef")
    private let crossRefDataID = Expression<Int64>("DataID_CaseXRef")

    private var theDataArray = [MyData]()
    private let theDataTable = Table("My_Data")
    private let dataID = Expression<Int64>("DataID")
    private let data_Name = Expression<String>("Data_Name")

    private var searchBarShowing: Bool = false
    private var isSearching: Bool = false
    private var searchingArray = [MyData]()
    private var theCurrentArray = [MyData]()

    private var theCaseDataArray = [Data_On_Case]()

    @IBOutlet weak var theSearchBar: UISearchBar!

    override func viewDidLoad()
    {
        super.viewDidLoad()

    }
}

extension Test_VC: UISearchBarDelegate
{
    func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int)
    {
        switch selectedScope
        {
        case 0:
            searchingArray = theDataArray
            isSearching.toggle()

        case 1:

           // Cannot convert value of type '((Data_On_Case) throws -> Bool) throws -> Bool' to expected argument type '(MyData) throws -> Bool'
            // The line below is where I'm stuck. The error is shown above.
            searchingArray = theDataArray.filter(theCaseDataArray.contains(where:  ))

            isSearching = true

            self.theSearchBar.resignFirstResponder()
            theSearchBar.showsScopeBar = true

        default:
            return
        }
    }
}