Using multiprocessing inside a class

I have a Python class that I use for a series of computations in which I am initialising a number of different pandas.DataFrames stored inside nested dictionaries. The bottleneck in initialising the class is as follows:
I have a regular grid of points spanning the Earth and a set of polygons (representing tectonic plates) that also cover the Earth. Of each point in the grid, I need to find in which plate they are located and assign the ID of that plate to a list.
Because this is relatively slow (i.e. takes 10s of seconds per iteration instead of <1s), I would like to parallelise this section (and only this section!) of the class. I am new to parallelisation, however, and I ran into some issues.

Here is the function that handles finding the plateID.

def process_plateIDs(
        geometries_data: list,
        lats_chunk: _numpy.array,
        lons_chunk: _numpy.array,
    ) -> list:
    plateIDs = _numpy.zeros(len(lats_chunk))
    
    for topology_geometry, topology_plateID in geometries_data:
        mask = topology_geometry.contains(_geopandas.points_from_xy(lons_chunk, lats_chunk))
        plateIDs[mask] = topology_plateID

    return plateIDs

Where I simplified the plate geometries that are originally stored in a geopandas.DataFrame to reduce the computation time:

def extract_geometry_data(topology_geometries):
    return [(geom, plateID) for geom, plateID in zip(topology_geometries.geometry, topology_geometries.PLATEID1)]

Note that each instance topology_geometries is stored in attribute of the main class self.topology_geometries, which is a dictionary that contains the geometries of the tectonic plates for different ages and cases (so self.topology_geometries[age][case] gives one geopandas.DataFrame).

I am trying to run this section in parallel by feeding it to Pool from multiprocessing:

# Use all available CPUs if num_workers is not specified
if num_workers is None:
   num_workers = _os.cpu_count()

# Split the data into chunks
chunk_size = len(lats) // num_workers
chunks = [(geometries_data.copy(), lats[i:i + chunk_size].copy(), lons[i:i + chunk_size].copy()) for i in range(0, len(lats), chunk_size)]

# Create a Pool of workers
with Pool(num_workers) as pool:
   # Map the process_chunk function to chunks
   results = pool.starmap(process_plateIDs, chunks)

# Concatenate results from all chunks
plateIDs = _numpy.concatenate(results)

However, this results in each worker reinitialising the entire class, which is not at all what I intended. I suspect this is because I am passing a view instead of separate copies of certain variables, which I unsuccessfully tried to prevent that by making explicit copies of each variable (as in the above code snippet).

How can I tell Python to only explicitly run process_plateIDs parallel, and keep the rest of the code sequential? It should run on a jupyter notebook.

Many thanks!

EDIT:
Additional information: I am running this on MacOS with M1 chip.

New contributor

thomas.s is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

MacOS uses spawn as a multiprocessing.set_start_method so you need your script to be like this, see Compulsory usage of if name==”main” in windows while using multiprocessing

import all_needed_modules

if __name__ == "__main__":
    do_actual_code()

this will prevent workers from running the code when they are importing your script.


the second problem is passing huge data around, all arguments are serialized and de-serialized each function call, so calling a function with big data blocks each time is bad, instead use a global and an initializer to set it, this initializer will only happen once per worker.

some_global_data = None
def set_global_data(data):
    global some_global_data
    some_global_data = data

...
...
#  much later, inside the "__main__" block
with Pool(num_workers, initializer=set_global_data, initargs=(geometries_data,)) as pool:
   # Map the process_chunk function to chunks
   results = pool.starmap(process_plateIDs, chunks)

and in your function just use this global instead of passing the huge data block each time.


lastly the problem of jupyter …. well, jupyter has very bad support for multiprocessing on any OS except linux due to the absence of fork, so you need the function that you are calling to be in another file that you import separately, see Jupyter notebook never finishes processing using multiprocessing

from some_file import process_plateIDs, set_global_data
# call pool.startmap(process_plateIDs, chunks) later

another alternative is to run jupyter through a linux container so python can use fork and you can declare the multiprocessed function directly within your notebook without the extra complications of jupyter.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật