2

我想創建一個線程,而在一個類內啓動另一個類的構造函數,但它看起來像pool.apply_async沒有像我期望的那樣傳遞kwargs。這裏是我的代碼(下調至僅包括線程代碼):Python多處理問題,而傳遞kwargs

from MyDatabaseScript import DB 

class Trinity: 
     def __init__(self): 

     #lets split a thread off and work on connecting to the mysql server 
     pool = ThreadPool(processes=1) #establish the thread pool 

     #I want to pass 'self' as an arg to DB because the Trinity instance has class variables that I want to use 
     args, kwargs = (self,), {"host":"my.server.name", "user": "databaseuser", "passwd": "xxxxxxxxxxx", "db": "database name"} 

     async_result = pool.apply_async(DB(), args, kwargs) #create the thread pool call for the DB class with the kwargs 

現在所有工作正常,我沒有得到任何錯誤,但在DB()方我的代碼看起來簡單,這:

import MySQLdb 

class DB: 
     def __init__(self, tms=None, **kwargs): 
      print tms, kwargs 

的問題是,__init__函數內的打印命令不打印任何東西,我得到這個:

None {} 

回答

3

ÿ ou呼叫DB而不是通過DBpool.apply_async

跌落()

async_result = pool.apply_async(DB, args, kwargs) 
+0

你是老闆! – sadmicrowave