在少数情况下,将collections.namedtuple与ProcessPoolExecutor一起使用会陷入困境

问题描述

    $user = '';
    $pass = '';
    $db = '';
    $port = '13306';
    $host = 'instance.sftp.wpengine.com';  //put your account name instead of 'instance' here
    $options = ['default-auth'=>'MysqL_native_password',PDO::MysqL_ATTR_SSL_CA=>__DIR__.'/wpengine_root_ca.pem',PDO::MysqL_ATTR_SSL_VERIFY_SERVER_CERT => false];
    
    $dbh = new PDO('MysqL:host='.$host.';port='.$port.';dbname='.$db.';default-auth=MysqL_native_password',$user,$pass,$options);

这里卡住了。

>>> import concurrent.futures
>>> from collections import namedtuple
>>> #1. Initialise namedtuple here
>>> # tm = namedtuple("tm",["pk"])  
>>> class T:  
...     #2. Initialise named tuple here
...     #tm = namedtuple("tm",["pk"]) 
...     def __init__(self): 
...         #3: Initialise named tuple here
...         tm = namedtuple("tm",["pk"])                       
...         self.x = {'key': [tm('value')]}  
...     def test1(self):  
...         with concurrent.futures.ProcesspoolExecutor(max_workers=1) as executor:  
...             results = executor.map(self.test,["key"])  
...         return results  
...     def test(self,s): 
...         print(self.x[s])   
... 
>>> t = T().test1()

如果我在类外部(在#1中)初始化命名的元组,则可以正常工作。有人可以让我知道如果我按照#2或#3进行初始化是什么问题吗?

解决方法

您没有更改初始化namedtuple的位置。您正在更改创建命名元组 class 的位置。

使用collections.namedtuple在模块“ y”中创建名为“ x”的namedtuple类时,其__module__设置为'y',其__qualname__设置为{ {1}}。酸洗和解酸依赖于此类实际上在这些属性指示的'x'位置中可用的此类,但是在您的示例的情况2和3中,不是。

Python无法腌制namedtuple,这会中断与工作人员的进程间通信。在工作进程中执行y.x依赖于在工作进程中腌制self.test并取消其副本的拷贝,如果self.test是可以的类的实例,则不会发生这种情况。不能腌。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...