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

ImportError: cannot import name Timeouterror #9

Closed
pw717 opened this issue Jun 6, 2017 · 5 comments
Closed

ImportError: cannot import name Timeouterror #9

pw717 opened this issue Jun 6, 2017 · 5 comments

Comments

@pw717
Copy link

pw717 commented Jun 6, 2017

With release 4.2.1 on Mac OSX : There is an error running your example on http://pythonhosted.org/Pebble/#examples
At the very beginning on the line
from concurrent.futures import Timeouterror

this throws ImportError error : cannot import name Timeouterror

Thank you in advance.

@noxdafox
Copy link
Owner

noxdafox commented Jun 7, 2017

That's a typo in the docs, it should be TimeoutError.

@pw717
Copy link
Author

pw717 commented Jun 7, 2017

Thank you, sorry for that, I should have found it...

I had to add from pebble.common import ProcessExpiredat the beginning.

The code is :

#!/usr/bin/env python                                                        
# -*- coding: utf-8 -*-                                                      
                                                                             
# From http://pythonhosted.org/Pebble/#examples                              
                                                                                                                                                        
from pebble import ProcessPool                                               
from pebble.common import ProcessExpired                                     
from concurrent.futures import TimeoutError                                  
                                                                             
def function(foo, bar=0):                                                    
    return foo + bar                                                         
                                                                             
elements = list(range(1000))                                                 
                                                                             
with ProcessPool() as pool:                                                  
    iterator = pool.map(function, elements, timeout=10)                      
    while True:                                                              
        try:                                                                 
            results = next(iterator)                                         
            print(results)                                                   
        except TimeoutError as error:                                        
            print("Function took longer than %d seconds" % error.args[1])    
        except ProcessExpired as error:                                      
            print("%s. Exit code: %d" % (error, error.exitcode))             
        except Exception as error:                                           
            print("function raised %s" % error)                              
            print(error.traceback)  # Python's traceback of remote process   
        except StopIteration:                                                
            break                                                            

Now the execution starts but at the end there are the two errors below :

function raised 
Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/site-packages/pebble/pool/process.py", line 155, in message_manager_loop
    pool_manager.process_next_message(SLEEP_UNIT)
  File "/usr/local/lib/python2.7/site-packages/pebble/pool/process.py", line 184, in process_next_message
    message = self.worker_manager.receive(timeout)
  File "/usr/local/lib/python2.7/site-packages/pebble/pool/process.py", line 304, in receive
    if self.pool_channel.poll(timeout):
  File "/usr/local/lib/python2.7/site-packages/pebble/pool/channel.py", line 44, in unix_poll
    return bool(select([self.reader], [], [], timeout)[0])
error: (9, 'Bad file descriptor')

and

Traceback (most recent call last):
  File "./travaux/multipro/test_pebble_2.py", line 29, in <module>
    print(error.traceback)  # Python's traceback of remote process
AttributeError: 'exceptions.StopIteration' object has no attribute 'traceback'

This leads to the second issue error: (9, 'Bad file descriptor') I wrote you yesterday about the ProcessPool class.

@noxdafox
Copy link
Owner

noxdafox commented Jun 7, 2017

I will update the documentation making sure all examples are correct.

Keep in mind that the order of exceptions is important. Exception is the most generic one, therefore it needs to be last.

from pebble import ProcessPool                                               
from pebble.common import ProcessExpired                                     
from concurrent.futures import TimeoutError                                  
                                                                             
def function(foo, bar=0):                                                    
    return foo + bar                                                         
                                                                             
elements = list(range(1000))                                                 
                                                                             
with ProcessPool() as pool:                                                  
    iterator = pool.map(function, elements, timeout=10)                      
    while True:                                                              
        try:                                                                 
            results = next(iterator)                                         
            print(results)                                                   
        except TimeoutError as error:                                        
            print("Function took longer than %d seconds" % error.args[1])    
        except ProcessExpired as error:                                      
            print("%s. Exit code: %d" % (error, error.exitcode))             
        except StopIteration:                                                
            break     
        except Exception as error:                                           
            print("function raised %s" % error)                              
            print(error.traceback)  # Python's traceback of remote process   

This logic won't raise the AttributeError anymore. For the other issue let's use the #10 to track it.

@pw717
Copy link
Author

pw717 commented Jun 7, 2017

Ok I understand !
Thank-you again.

noxdafox added a commit that referenced this issue Jun 7, 2017
Signed-off-by: Matteo Cafasso <noxdafox@gmail.com>
noxdafox added a commit that referenced this issue Jun 7, 2017
Signed-off-by: Matteo Cafasso <noxdafox@gmail.com>
@noxdafox
Copy link
Owner

noxdafox commented Jun 7, 2017

Fixed in release 4.3.1.

@noxdafox noxdafox closed this as completed Jun 7, 2017
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

2 participants