如何编写使用两个队列的Python多进程脚本?
我基本上需要根据我在初始项目中发现的内容,在工作队列中添加更多任务。我在下面发布的示例很愚蠢(我可以随意更改项目并将其直接放在输出Queue中),但是它的机制很明确,反映了我需要开发的概念的一部分。
在此,我尝试:
import multiprocessing as mp
def worker(working_queue, output_queue):
item = working_queue.get() #I take an item from the working queue
if item % 2 == 0:
output_queue.put(item**2) # If I like it, I do something with it and conserve the result.
else:
working_queue.put(item+1) # If there is something missing, I do something with it and leave the result in the working queue
if __name__ == '__main__':
static_input = range(100)
working_q = mp.Queue()
output_q = mp.Queue()
for i …Run Code Online (Sandbox Code Playgroud) 我有两个不同长度的数组。一个包含匀称的多边形,另一个包含匀称的点。我想为两个数组中的每个可能的元素组合运行 a_polygon.contains(a_point) 匀称的函数。
我正在看这篇文章,因为构建一个包含行中所有可能组合的两列矩阵可能是一个理想的中间步骤。但是当输入数据很大时,'cartersian(arrays)' 函数中的循环可能会影响性能。
我尝试广播其中一个数组,然后应用 shapely 函数:
Polygons_array[:,newaxis].contains(Points_array)
Run Code Online (Sandbox Code Playgroud)
但这当然是行不通的。我知道最近发布的 geopandas 库,但它不是我的 Canopy 安装的一个选项。