繁体   English   中英

传递kwargs时出现Python多处理问题

[英]Python Multiprocessing issue while passing kwargs

我正在尝试在一个类中创建一个线程以启动另一个类构造函数,但似乎pool.apply_async没有像我期望的那样传递pool.apply_async 这是我的代码(整理为仅包含线程代码):

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__函数中的print命令不打印任何内容,我得到以下信息:

  None {} 

您正在调用DB而不是将DB传递给pool.apply_async

删除()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM