Python3:如何检测是否可以使用/ dev / shm(或ProcessPoolPexecutor)?

时间:2019-11-05 09:02:41

标签: python-3.x

我有一些可以通过多处理得到改进的代码,但是在AWS Lambda中,/dev/shm不可用,因此ProcessPoolExecutor失败,并显示了一条错误消息:

 File "/var/task/black.py", line 529, in reformat_many
    executor = ProcessPoolExecutor(max_workers=worker_count)
  File "/var/lang/lib/python3.7/concurrent/futures/process.py", line 556, in __init__
    pending_work_items=self._pending_work_items)
  File "/var/lang/lib/python3.7/concurrent/futures/process.py", line 165, in __init__
    super().__init__(max_size, ctx=ctx)
  File "/var/lang/lib/python3.7/multiprocessing/queues.py", line 42, in __init__
    self._rlock = ctx.Lock()
  File "/var/lang/lib/python3.7/multiprocessing/context.py", line 67, in Lock
    return Lock(ctx=self.get_context())
  File "/var/lang/lib/python3.7/multiprocessing/synchronize.py", line 162, in __init__
    SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)
  File "/var/lang/lib/python3.7/multiprocessing/synchronize.py", line 59, in __init__
    unlink_now)
OSError: [Errno 38] Function not implemented

是否有一种便携式方法可以检测到它会失败?

1 个答案:

答案 0 :(得分:1)

您可以使用try / except,但例外:https://docs.python.org/fr/3/tutorial/errors.html