gpt4 book ai didi

python - 在 AWS Lambda 函数中使用 Python 多处理队列

转载 作者:行者123 更新时间:2023-11-30 21:53:32 28 4
gpt4 key购买 nike

我有一些Python可以创建多个进程来更快地完成任务。当我创建这些进程时,我会传入一个队列。在进程内部,我使用queue.put(data),因此我能够检索进程外部的数据。它在我的本地计算机上运行得非常好,但是当我将 zip 上传到 AWS Lambda 函数 (Python 3.8) 时,它说 Queue() 函数尚未实现。当我只需取出队列时,该项目在 AWS Lambda 中运行得很好功能,所以我知道这是我目前唯一的挂断。

我确保使用“pip install multiprocess -t ./”以及“pip install boto3 -t ./”将多处理包直接安装到我的Python项目中。

我对 Python 和 AWS 都是新手,但我最近遇到的研究可能会将我们指向 SQS。

阅读这些 SQS docs我不确定这是否正是我正在寻找的。

以下是我在 Lambda 中运行的代码,该代码在本地运行,但不能在 AWS 上运行。请参阅带 * 的重要部分:

from multiprocessing import Process, Queue
from craigslist import CraigslistForSale
import time
import math

sitesHold = ["sfbay", "seattle", "newyork", "(many more)..." ]

results = []


def f(sites, category, search_keys, queue):
local_results = []
for site in sites:
cl_fs = CraigslistForSale(site=site, category=category, filters={'query': search_keys})
for result in cl_fs.get_results(sort_by='newest'):
local_results.append(result)
if len(local_results) > 0:
print(local_results)
queue.put(local_results) # Putting data *********************************


def scan_handler(event, context):
started_at = time.monotonic()
queue = Queue()
print("Running...")
amount_of_lists = int(event['amountOfLists'])
list_length = int(len(sitesHold) / amount_of_lists)
extra_lists = math.ceil((len(sitesHold) - (amount_of_lists * list_length)) / list_length)
site_list = []
list_creator_counter = 0
site_counter = 0
for i in range(amount_of_lists + extra_lists):
site_list.append(sitesHold[list_creator_counter:list_creator_counter + list_length])
list_creator_counter += list_length
processes = []
for i in range(len(site_list)):
site_counter = site_counter + len(site_list[i])
processes.append(Process(target=f, args=(site_list[i], event['category'], event['searchQuery'], queue,))) # Creating processes and creating queues ***************************

for process in processes:
process.start() # Starting processes ***********************

for process in processes:
listings = queue.get() # Getting from queue ****************************
if len(listings) > 0:
for listing in listings:
results.append(listing)

print(f"Results: {results}")

for process in processes:
process.join()

total_time_took = time.monotonic() - started_at
print(f"Sites processed: {site_counter}")
print(f'Took {total_time_took} seconds long')

这是 Lambda 函数给我的错误:

{
"errorMessage": "[Errno 38] Function not implemented",
"errorType": "OSError",
"stackTrace": [
" File \"/var/task/main.py\", line 90, in scan_handler\n queue = Queue()\n",
" File \"/var/lang/lib/python3.8/multiprocessing/context.py\", line 103, in Queue\n return Queue(maxsize, ctx=self.get_context())\n",
" File \"/var/lang/lib/python3.8/multiprocessing/queues.py\", line 42, in __init__\n self._rlock = ctx.Lock()\n",
" File \"/var/lang/lib/python3.8/multiprocessing/context.py\", line 68, in Lock\n return Lock(ctx=self.get_context())\n",
" File \"/var/lang/lib/python3.8/multiprocessing/synchronize.py\", line 162, in __init__\n SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)\n",
" File \"/var/lang/lib/python3.8/multiprocessing/synchronize.py\", line 57, in __init__\n sl = self._semlock = _multiprocessing.SemLock(\n"
]
}

Queue() 在 AWS Lambda 中工作吗?实现我的目标的最佳方法是什么?

最佳答案

关于python - 在 AWS Lambda 函数中使用 Python 多处理队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638035/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com