【问题标题】:Share requests.Session between processes in Python在 Python 中的进程之间共享 requests.Session
【发布时间】:2017-02-08 18:22:14
【问题描述】:

我想通过多处理在多个 python 进程之间共享稍微修改过的 requests.Session 版本,但奇怪的事情发生在我身上。通过 Queue 和 Manager,我得到的 requests.Session 对象都被删除了我的修改。

 s = requests.Session()
 s._my_custom_field = "test"

 q = multiprocessing.Queue()
 q.put(s)
 s_from_queue = q.get(s)
 s_from_queue._my_custom_field

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Session' object has no attribute '_my_custom_field'

同样的事情发生在经理身上。任何想法为什么会发生这种情况以及我应该如何在进程之间共享 requests.Session?谢谢。

【问题讨论】:

    标签: python python-requests python-multiprocessing


    【解决方案1】:

    由于 IO 不是 CPU 密集型的,您可以考虑使用 multiprocessing.dummy,它使用线程而不是进程。这样您就可以跨线程在内存中使用相同的 Session。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      相关资源
      最近更新 更多