Python's concurrent.futures.ProcessPoolExecutor is equipped to handle a high volume of tasks with its extensive RAM capabilities

When running Python codes in parallel using concurrent.futures.ProcessPoolExecutor, I noticed that for larger values of n, there was a significant increase in RAM usage. Upon further investigation, it seemed that storing futures set (or list) was contributing to this issue. In an attempt to reduce RAM consumption, I modified the code by not storing the futures set and performing operations directly in my_function like so:

with concurrent.futures.ProcessPollExecutor(max_workers=10) as executor:
    for i in range(n):
        executor.submit(my_function, i)

However, this approach still resulted in high RAM usage. After some research, I came across an issue on Python's bug tracker which explained that submitting all tasks at once can lead to tasks being stored in memory until execution completes.

To address this problem without introducing delays in the loop, I sought a better method to submit tasks gradually as previous ones are completed. I considered using the map method instead of submit, but was unsure about the purpose of the chunksize argument and how to determine its value.

Having previously used GNU Parallel without encountering similar RAM issues, I am looking for a Python-only solution that optimizes memory usage. Is there a more efficient or elegant way to achieve this parallel processing while minimizing RAM consumption?

Answer №1

To solve the issue, consider using queues to restrict the number of concurrent futures waiting or implement periodic pooling for processed futures to send them in batches rather than all at once.

Referring to this article may provide valuable insights into resolving your specific problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The server is unable to process the request sent by the browser or proxy. This error occurred in a Flask web application

Can someone guide me on troubleshooting this issue in Flask? I am still learning. Server running at (Press CTRL+C to exit) 127.0.0.1 - - [26/Jul/2020 11:19:45] "GET /predict HTTP/1.1" 500 - Traceback (most recent call last): raise exceptions. ...

How can we generate Kivy widgets without relying on the kv language?

Is there a method to develop Kivy widgets without relying on kv language and .kv files? I am new to Kivy, coming from a background in using Tkinter. I have been exploring Kivy for its potential in creating Android applications. While I understand that Kivy ...

Utilizing a Trained TensorFlow Model with Python: A Step-by-Step Guide

After training a model in TensorFlow using the tf.estimator API, specifically with tf.estimator.train_and_evaluate, I now have the output directory of the training data. How can I effectively load my model from this directory and start using it? I attempt ...

Click on a previously saved Selenium element once the page has been reloaded

Is there a way to achieve a similar functionality where each "button" press triggers a page reload? element = driver.find_element_by_xpath("//select[@name='name']") all_options = element.find_elements_by_tag_name("option") for option in all_opti ...

"Creating a lambda function with multiple conditional statements for data manipulation in a Pand

I have a basic dataset containing revenue and cost values. In my particular case, the cost figures can sometimes be negative. My goal is to calculate the ratio of revenue to cost using the following formula: if ((x['cost'] < 0) & (x[&apo ...

moving data from tkinter entry fields

Is it possible to access the output of an entry field created within one function in another function? def test1(): registerWindow = Toplevel() username_entry = Entry(registerWindow, width=30) username_entry.pack() register_btn = Button(r ...

Node.js (npm) is still unable to locate python despite setting %PYTHON% beforehand

Trying to get Node.js to work is proving to be more challenging than expected! Despite having two versions of Python on my computer, it seems that Node.js only works with the older version, 2.7. When I encountered an error, it prompted me to set the path ...

Exploring deep differences by displaying all keys in case of any mismatches

I am currently utilizing deepdiff to compare data from two different databases. Here's an example: from deepdiff import DeepDiff users1 = [{'id': 1, 'name': 'John', 'age': 30}, {'id': 2, 'name&apo ...

Retrieving and saving images from Azure Blob Storage

I need help with exporting matplotlib plots from Databricks to Blob Storage. Currently, I am using the following code: plt.savefig('/dbfs/my_plot.png') dbutils.fs.cp('dbfs:my_plot.jpg', blob_container) However, the issue arises when I ...

When trying to load the JSON data saved in a JSON file, I encounter an error: JSONDecodeError: Expecting value at line 1, column 1

In this scenario, I am experimenting with saving data in json format to a json file, but encountering issues when trying to load it back. import json # Python objects can be stored in json format value = [ ['sentence one', {'en ...

SQLAlchemy is unable to generate relational column connections

Thank you in advance. I'm currently working on an application using Flask and SQLAlchemy. When I first started, I initialized the database by running flask db init. $ flask db init Creating directory /Users/xxxx/app/migrations ... done Creating ...

Performing a search in Django using raw MySQL commands

I am currently in the process of developing a custom search engine that includes 4 search fields, aiming to search within a MySQL table. This snippet from my views.py covers the search functionality, pagination, and listing of the entire table data. def ...

Creating unique characters with Python Selenium

I am interested in creating drawings of characters A B C D on the canvas at using Selenium Action chains. import time from selenium.webdriver.common.action_chains import ActionChains def draw(action, offset_list): for offset in offset_list: a ...

Using Python Imaging Library (PIL) to preserve a multipage tiff

Is there a way to convert a list of numpy arrays to an image object that PIL can recognize? I know you can save a multipage tiff file using the im.save(filepath, save_all=True) method in PIL, but how do you create the image object from a list of numpy ar ...

Pandas allows you to visualize two dataframes on a single plot with x-axis ticks positioned both on the top and

Is it possible to plot two dataframes with different x labels on one graph without the second set of x labels overriding the first? How can I move the second set of x labels to the top? #!/usr/bin/env python3 import matplotlib.pyplot as plt import pandas a ...

Exporting ANSYS API DataTable to Excel Spreadsheet

After obtaining an array of data (B=[1,2,3,4,5]) from a DataTable, I attempted to use a Python for loop to import it into an Excel file with the following code: def Cells(a,b): return str(chr(b+96) + str(a)) import clr clr.AddReference("Microsoft. ...

Interpreting JSON bytes as a literal string representation

Currently, I am facing an issue with a previously created jsonl file. The problem lies in the fact that I mistakenly saved the encoded bytes literal as 'b'{"foo": "Don\\u2019t", "bar": "bar"}&apos ...

PhantomJS with Selenium encounters ElementNotVisible error when selecting a combo box, whereas Firefox performs without any issues in the same

I am working on a website that features a unique combo box where items are selected from divs instead of traditional options. My program needs to click on the combo box, wait (I've found using implicitly_wait(3) for 3 seconds works best), and then sel ...

Difficulty integrating a Python solution with a C++ component in Visual Studio 2017

When attempting to open the twslink2pt.sln project/solution (a Python 3.6 wrapper with a c++ component) using Visual Studio 2017, an error message is displayed. 1>------ Build started: Project: twslink2pt, Configuration: Release Win32 ------ 1>Per ...

Typing into a form field using Selenium

I'm having trouble interacting with the payment information input fields. It seems they are nested within iframes, and I cannot switch between frames successfully. Attached is a snapshot of the HTML code and the corresponding webpage. Any assistance i ...