Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add temporary-file-path argument for multi-processing #19

Merged
merged 3 commits into from
May 10, 2024

Conversation

iwyoo
Copy link
Contributor

@iwyoo iwyoo commented May 10, 2024

Thank you for the great repo!

I found that using a single predefined temp-file-path may cause error because of race condition. This patch simply resolves the issue by adding option (tmp_fpath).

import os
from multiprocessing import Process, Queue
from py2opsin import py2opsin
import tqdm


def worker(tmp_fpath, q1, q2):
    while True:
        data = q1.get()

        if data is None:
            break

        smiles = py2opsin(data, tmp_fpath=tmp_fpath)
        q2.put(smiles)


if __name__ == "__main__":
    n_processes = 4
    os.makedirs("tmp", exist_ok=True)
    tmp_fpaths = [f"tmp/{i}.txt" for i in range(n_processes)]

    q1 = Queue()
    q2 = Queue()

    processes = [Process(target=worker, args=(tmp_fpaths[i], q1, q2)) for i in range(n_processes)]
    [p.start() for p in processes]

    for i in tqdm.tqdm(range(100)):
        q1.put("2,4,6-trinitrotoluene")

    for _ in tqdm.tqdm(range(100)):
        smiles = q2.get()
        assert smiles == "[N+](=O)([O-])C1=C(C)C(=CC(=C1)[N+](=O)[O-])[N+](=O)[O-]", smiles

    for _ in range(n_processes):
        q1.put(None)

    [p.join() for p in processes]

@JacksonBurns
Copy link
Owner

Thanks for the PR! I am going to update the doc string and tack on a unit test, then merge.

@JacksonBurns
Copy link
Owner

A word of caution, too - as you see mentioned in the OPSIN repo (dan2097/opsin#214 (comment)) the underlying OPSIN software will already parallelize itself. Be wary when using multiprocessing in Python that you might end up slowing yourself down if you spawn too many processes and then end up subsequently spawning too many threads.

@JacksonBurns JacksonBurns self-requested a review May 10, 2024 18:16
Copy link
Owner

@JacksonBurns JacksonBurns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again!

@JacksonBurns JacksonBurns merged commit 6f94362 into JacksonBurns:main May 10, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants