Reputation: 40892
I'm trying to create a thread while within a class to start another class constructor but it seems like pool.apply_async
isn't passing the kwargs like I would expect it to. Here is my code (trimmed down to only include the threading code):
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
Now that all works fine and I don't get any errors, but on the DB() side my code looks simple and is this:
import MySQLdb
class DB:
def __init__( self, tms=None, **kwargs ):
print tms, kwargs
The issue is that the print command within the __init__
function doesn't print anything, I get this:
None {}
Upvotes: 2
Views: 1766
Reputation: 369044
You are calling DB
instead of passing DB
to pool.apply_async
.
Drop ()
:
async_result = pool.apply_async(DB, args, kwargs)
Upvotes: 3