Skip to content Skip to sidebar Skip to footer

Trying To Run A Defined Function With A Delay

I am trying to incrementally load values from the first column of a csv file into a URL and request the URL one at a time with a 5 second delay. Each value in the first column sho

Solution 1:

Your withid() function never returns anything. Try adding return movdata at the end of it.

Here is a rewritten version which may be helpful:

import csv
import json
import time
import urllib2

PAGE_DELAY = 5.# time between loading pages
PAGE_LOAD  = 0.3# how long it takes to load a page

make_url = 'http://www.imdb.com/title/tt{}/'.formatdefget_csv_column(csv_fname, col, **kwargs):
    withopen(csv_fname, 'rb') as inf:
        incsv = csv.reader(inf, **kwargs)
        column = [row[col] for row in incsv]
    return column

defget_data_by_id(id):
    url = make_url(id)
    response = urllib2.urlopen(url)
    data = json.load(response)
    returnid,data

defdelayed(delay, fn, *args):
    time.sleep(delay)
    return fn(*args)

defhuman_time(seconds):
    if seconds >= 86400:
        return'{:0.1f} days'.format(seconds / 86400.)
    elif seconds >= 3600:
        return'{:0.1f} hours'.format(seconds / 3600.)
    elif seconds >= 60:
        return'{:0.1f} minutes'.format(minutes / 60.)
    else:
        return'{:0.1f} seconds'.format(seconds)

defmain():
    ids = get_csv_column('outputrows2.csv', 0)

    expected = (PAGE_DELAY + PAGE_LOAD) * len(ids)
    print('This will take about {}.'.format(human_time(expected)))

    results = (delayed(PAGE_DELAY, get_data_by_id, id) foridin ids)
    moviedata = dict(results)     # => gives dict of {id:data}if __name__=="__main__":
    main()

Post a Comment for "Trying To Run A Defined Function With A Delay"