2

我试图在一个类中创建一个线程来启动另一个类构造函数,但它似乎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 {} 
4

1 回答 1

3

你打电话DB而不是传递DBpool.apply_async.

掉落()

async_result = pool.apply_async(DB, args, kwargs)
于 2013-08-27T16:05:24.790 回答