我正在尝试在类中创建一个线程来启动另一个类构造函数,但似乎pool.apply_async
没有像我期望的那样传递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 {}
答案 0 :(得分:3)
您正在呼叫DB
,而不是将DB
传递给pool.apply_async
。
删除()
:
async_result = pool.apply_async(DB, args, kwargs)