1

I am using multiprocessing in python, try to kill the running after a timeout. But it doesn't work, and I don't know the reason.

I followed an example, it seems easy. Just need to start the process, after 2 seconds, terminate the running. But it doesnt work for me.

Could you please help me figure it out? Thanks for your help!

from amazonproduct import API
import multiprocessing
import time



AWS_KEY = '...'
SECRET_KEY = '...'
ASSOC_TAG  = '...'


def crawl():

    api = API(AWS_KEY, SECRET_KEY, 'us', ASSOC_TAG)

    for root in api.item_search('Beauty', Keywords='maybelline',
                                ResponseGroup='Large'):
        # extract paging information
        nspace = root.nsmap.get(None, '')
        products = root.xpath('//aws:Item', 
                             namespaces={'aws' : nspace})

        for product in products:
            print product.ASIN,

if __name__ == '__main__':
    p = multiprocessing.Process(target = crawl())
    p.start()
    if time.sleep(2.0):
        p.terminate()

1 Answer 1

3

Well, this won't work:

if time.sleep(2.0):
    p.terminate()

time.sleep does not return anything, so the above statement is always equivalent to if None:. None is False in a boolean context, so there you go.

If you want it to always terminate, take out that if statement. Just do a bare time.sleep.

Also, bug:

p = multiprocessing.Process(target = crawl())

This isn't doing what you think it's doing. You need to specify target=crawl, NOT target=crawl(). The latter calls the function in your main thread, the former passes the function as an argument to Process which will then execute it in parallel.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.