Does processes triggered by ProcessPoolExecutor.submit have any kind of interference with each other?

Background:

.
├── app.py # API source code file
├── worker.py # CPU-bound task

I am using ProcessPoolExecutor().submit() in app.py to execute an entrance function main() stored in worker.py. As long as the worker process is triggered, the process id of it will be returned to app process via a Queue created from Manager.

My original source code is like this:

app.py:

from concurrent.futures import ProcessPoolExecutor
from multiprocessing import Manager

from fastapi import FastAPI
from loguru import logger

from worker import main

app = FastAPI()


executor = ProcessPoolExecutor()
queue = Manager().Queue() # A queue created via Manager is used for message exchange.


@app.get("/whatever-route")
def whatever-route():
    logger.info(f"Before submitting")
    executor.submit(main, queue) # submit the CPU-bound task
    sub_pid = queue.get()
    logger.info(f"Got sub pid from queue.")
    return sub_pid

worker.py:

... # Three dots here means there are many packages to be imported.

logger.info("Finished importing packages.")


def init():
    pass


def main(q: Queue):
    sub_pid = os.getpid()
    q.put(sub_pid)
    logger.info(f"Put sub pid into queue.")
    ... # Three dots here means additional CPU-bound task to be executed.
    return 1

When debugging the api, I noticed that it takes sooo much time in importing the packages in worker.py:

2024-06-25 17:54:23.661 | INFO | app:whatever_route:26 – Before submitting

2024-06-25 17:54:25.146 | INFO | worker::33 – Finished importing packages.

2024-06-25 17:54:25.151 | INFO | worker:main:44 – Put sub pid into queue.

2024-06-25 17:54:25.151 | INFO | app:whatever_route:29 – Got sub pid from queue.


One feasible solution:

After realising the problem, my motivation is to try executing import clauses in worker.py once app.py is executed, instead of being put off to the stage when literally executing the task. Therefore, I added a line in app.py, after created the executor. I tried it on, it works.

app.py(modified):

... # Remain unchanged.


executor = ProcessPoolExecutor()
executor.submit(init) # NEWLY ADDED LINE. init is a function in worker.py which does nothing.
queue = Manager().Queue() # A queue created via Manager is used for message exchange.


@app.get("/whatever-route")
... # Remain unchanged

2024-06-25 18:14:59.382 | INFO | worker::33 – Finished importing packages.

2024-06-25 18:19:42.704 | INFO | app:whatever_route:29 – Before submitting

2024-06-25 18:19:42.721 | INFO | worker:main:44 – Put sub pid into queue.

2024-06-25 18:19:42.721 | INFO | app:whatever_route:32 – Got sub pid from queue.


My Question:

Though the time overhead issue has been solved, I am quite confused on why my newly added line makes improvement. Given that executor.submit(init) is submitted to process A, and executor.submit(main, queue) should be submitted to a separate process B, process B should not be interfered by A and it should execute import clauses again?What is the underlying principle behind this improvement?

Apart from your guidance, any links to python official docs are highly welcomed!

New contributor

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

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