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

Only import tkinter if a GUI is needed #3348

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

samertm
Copy link

@samertm samertm commented Dec 18, 2024

Only import tkinter if the user is calling a function that explicitly uses a GUI instead of at the top-level.

This avoids the issue on macOS where calling fork after importing nltk without using any gui-related features causes the program to segfault.

Also adds the env var NLTK_DOWNLOADER_FORCE_INTERACTIVE_SHELL which forces the use of the interactive shell when using interactive downloader if set to 'true'.

Fixes #2949.

Tested that the reproducing program no longer crashes:

import torch
from torch.utils.data import DataLoader, TensorDataset
import os
import torch.multiprocessing as mp

import nltk

if __name__ == "__main__":
    if mp.get_start_method(allow_none=True) is None and os.name != 'nt':
        mp.set_start_method('fork')

    train = TensorDataset(torch.ones((50, 5)))

    pipe = DataLoader(dataset=train, batch_size=5, num_workers=1)
    itr = iter(pipe)
    data = next(itr)
    print(data)

Tested that the nltk downloader GUI still works when explicitly requested.

Tested that the nltk.sem.drt draw demos still work.

Only import tkinter if the user is calling a function that explicitly
uses a GUI instead of at the top-level.

This avoids the issue on macOS where calling fork after importing nltk
without using any gui-related features causes the program to segfault.

Also adds the env var NLTK_DOWNLOADER_FORCE_INTERACTIVE_SHELL which
forces the use of the interactive shell when using interactive
downloader if set to 'true'.

Fixes nltk#2949.

Tested that the reproducing program no longer crashes:

```
import torch
from torch.utils.data import DataLoader, TensorDataset
import os
import torch.multiprocessing as mp

import nltk

if __name__ == "__main__":
    if mp.get_start_method(allow_none=True) is None and os.name != 'nt':
        mp.set_start_method('fork')

    train = TensorDataset(torch.ones((50, 5)))

    pipe = DataLoader(dataset=train, batch_size=5, num_workers=1)
    itr = iter(pipe)
    data = next(itr)
    print(data)
```

Tested that the nltk downloader GUI still works when explicitly
requested.

Tested that the nltk.sem.drt draw demos still work.
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.

Importing NLTK breaks PyTorch multiprocessing on Mac
1 participant