user3026143
user3026143

Reputation: 65

Python Multiprocessing Timeout problems

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()

Upvotes: 1

Views: 1253

Answers (1)

roippi
roippi

Reputation: 25974

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.

Upvotes: 3

Related Questions