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

NameError: name 'time' is not defined #39

Open
FlorinAndrei opened this issue Sep 6, 2021 · 3 comments
Open

NameError: name 'time' is not defined #39

FlorinAndrei opened this issue Sep 6, 2021 · 3 comments

Comments

@FlorinAndrei
Copy link

This code breaks with NameError: name 'time' is not defined:

import time
from tqdm.auto import tqdm
from p_tqdm import p_map, p_umap, p_imap, p_uimap

numbers = list(range(0, 1000))

def heavy_processing(number):
    time.sleep(0.05)
    output = number + 1
    return output

results = p_map(heavy_processing, numbers)

print(results)

Error message:

RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "C:\Users\flori\AppData\Roaming\Python\Python38\site-packages\multiprocess\pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "C:\Users\flori\AppData\Roaming\Python\Python38\site-packages\pathos\helpers\mp_helper.py", line 15, in <lambda>
    func = lambda args: f(*args)
  File "<ipython-input-2-a99c335b4b30>", line 5, in add
NameError: name 'time' is not defined
"""

The above exception was the direct cause of the following exception:

NameError                                 Traceback (most recent call last)
<ipython-input-4-1bbf104dda25> in <module>
----> 1 added = p_map(add, l1, l2)

~\AppData\Roaming\Python\Python38\site-packages\p_tqdm\p_tqdm.py in p_map(function, *iterables, **kwargs)
     58     ordered = True
     59     generator = _parallel(ordered, function, *iterables, **kwargs)
---> 60     result = list(generator)
     61 
     62     return result

~\AppData\Roaming\Python\Python38\site-packages\p_tqdm\p_tqdm.py in _parallel(ordered, function, *iterables, **kwargs)
     47     map_func = getattr(pool, map_type)
     48 
---> 49     for item in tqdm(map_func(function, *iterables), total=length, **kwargs):
     50         yield item
     51 

~\AppData\Roaming\Python\Python38\site-packages\tqdm\notebook.py in __iter__(self)
    252     def __iter__(self):
    253         try:
--> 254             for obj in super(tqdm_notebook, self).__iter__():
    255                 # return super(tqdm...) will not catch exception
    256                 yield obj

~\AppData\Roaming\Python\Python38\site-packages\tqdm\std.py in __iter__(self)
   1176 
   1177         try:
-> 1178             for obj in iterable:
   1179                 yield obj
   1180                 # Update and possibly print the progressbar.

~\AppData\Roaming\Python\Python38\site-packages\multiprocess\pool.py in next(self, timeout)
    866         if success:
    867             return value
--> 868         raise value
    869 
    870     __next__ = next                    # XXX

NameError: name 'time' is not defined

However, this works fine - the difference being, I've passed the time module as a name to my function:

import time
from tqdm.auto import tqdm
from p_tqdm import p_map, p_umap, p_imap, p_uimap

numbers = list(range(0, 1000))

def heavy_processing(number, time=time):
    time.sleep(0.05)
    output = number + 1
    return output

results = p_map(heavy_processing, numbers)

print(results)

I have Windows 10, Jupyter Notebook, Python 3.8.8, and these packages:

p-tqdm==1.3.3
tqdm==4.61.1
pathos==0.2.8
multiprocess==0.70.12.2

Is it a bug with p-tqdm? Or with one of the other modules? (in which case I will move this bug report to the appropriate repo)

@seangjones
Copy link

@FlorinAndrei ,

I am facing the same issue with a similar usage. I have imported the time module, and inside a function running in a couple parallel processes, I am attempting a time.sleep(1). When that line is reached in each process, I get the NameError: name 'time' is not defined error you're reporting.

I'm also running Jupyter on Win 10, so I suspect a similar root cause. Did you uncover a solution?

@Shravan1804
Copy link

This error is due to a different forking behavior in windows.
You should add the following after importing p_tqdm:

import dill
dill.settings['recurse'] = True

Check this discussion for more details.

@seangjones
Copy link

Worked like a charm!

Thanks a million!

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

No branches or pull requests

3 participants