微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我需要将什么类型传递给表对象的 multiprocessing.Value

如何解决我需要将什么类型传递给表对象的 multiprocessing.Value

我有一个 table 对象,我想将它传递给多个线程。我使用 multiprocessing.Value 函数为该对象创建一个信号量。但是,它告诉我 Float32Atom 不可哈希。不知道在这种情况下该怎么做?

>>> import tables as tb
>>> f = tb.open_file('dot.h5','w')
>>> filters = tb.Filters(complevel=5,complib='blosc')
>>> n_ = 10000
>>> W_hat = f.create_carray(f.root,'data',tb.Float32Atom(),shape=(n_,n_),filters=filters)
>>> W_hat = Value(tb.Float32Atom(),W_hat)
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  File "/home/lib/python3.8/multiprocessing/context.py",line 135,in Value
    return Value(typecode_or_type,*args,lock=lock,File "/home/lib/python3.8/multiprocessing/sharedctypes.py",line 74,in Value
    obj = RawValue(typecode_or_type,*args)
  File "/home/lib/python3.8/multiprocessing/sharedctypes.py",line 48,in RawValue
    type_ = typecode_to_type.get(typecode_or_type,typecode_or_type)
TypeError: unhashable type: 'Float32Atom'

解决方法

如果你只有线程(没有进程)是正确的,你可以只使用 multiprocessing.Semaphore。所有线程都在同一上下文中运行,因此您可以将其用于所有线程。

https://docs.python.org/3/library/threading.html#semaphore-objects

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。