Heroku WebHook查询和功能

时间:2017-03-22 18:10:34

标签: python json azure heroku webhooks

使用Zapier,我使用来自客户端的以下数据发送了一个JSON对象:

fields = {
    “data":[
        {
            "duration":4231,
            “description”:”text text text "
        },
        {
            "duration":283671,
            “description”:”text text text "
        },
        {
            "duration":233671,
            “description”:”text text text "
        },
        {
            "duration":293671,
            “description”:”text text text "
        }
    ]
}

当我发送一个类似于此的发送JSON对象时,它可能会收到一个甚至可能超过100个元素的数组。在服务器端,我需要它来获取这些数据并通过对象数组并设置一个计时器,以具有与“duration”属性相同的持续时间。我在Python中有一个示例脚本来说明我的意思。

Server Side:
import requests
import json
import time

class Person(object):
    def __init__(self, url=None, duration=None):
        self.url = url
        self.duration = duration
string = input['data2']
output = [{'id': 123, 'hello': 'world'}]
print(input['data2'])
print(len(string))
holder = string.splitlines()
print(holder)
tempd = ""
insert = 0
for x in holder:
    if len(x) > 0:
        if(x.find("duration") != -1):
            tempA = x.split(":")#its techincally reading the property as a string so its whatever I split it and get the number out
            timer = (float(tempA[1])/1000)#converting the miliseconds to seconds
            print(timer)
            print "Start : %s" % time.ctime()
            #time.sleep(timer)#based on the duration determines how long my timer will sleep for
            print "End : %s" % time.ctime()

当我执行这个脚本时,我遇到超时问题,但我需要服务器做的只是发送一个响应,它已收到请求并正在执行带有所需JSON对象的脚本?我需要这一天全天去,我只是说这个阵列可以包含100个元素,每个元素之间至少有2分钟的延迟。 Heroku是否会支持这种需求,还是应该在Azure上构建服务器?

1 个答案:

答案 0 :(得分:0)

Heroku终止请求after 30 seconds。但是,您可以运行更新数据存储的后台任务,然后在页面加载时检查数据存储。 以下是使用threadingdatasetsqlite进行本地测试的最小示例。在heroku上,你必须用这个postgres服务之类的云数据存储替换sqlite引用。直接SQLAlchemy通常比数据集更受欢迎。

from flask import Flask, jsonify
import dataset
import threading
import time
import random

app = Flask(__name__)
DATABASE_URL = 'sqlite:///dev.db'

def add_person(name):
    """ Add a person to the db. """
    person = {'name': name, 'age': -1, 'status': 'processing'}
    db = dataset.connect(DATABASE_URL)
    db['people'].insert(person)
    return True

def update_person(name):
    """ Update person in db. """
    age = random.randint(1, 120)  # make your api call here
    time.sleep(10)  # simulate long running process
    person = {'name': name, 'age': age, 'status': 'finished'}
    db = dataset.connect(DATABASE_URL)
    db['people'].update(person, ['name'])
    return True

def get_person(name):
    """ Retrieve a person from the db. """
    db = dataset.connect(DATABASE_URL)
    person = db['people'].find_one(name=name)
    return person

@app.route('/<name>')
def index(name):
    """ If name not found, add_person to db and start update_person thread. 
    Return a person from db. """
    if not get_person(name):
        add_person(name)
        thread = threading.Thread(target=update_person, args=(name,))
        thread.start()
    person = get_person(name)
    return jsonify(person)

if __name__ == '__main__':
    app.run(debug=True)

访问127.0.0.1:5000/tom,当thread正在运行时,您将获得:

{
    "age": -1,
    "id": 14,
    "name": "tom",
    "status": "processing"
}

thread完成后重新加载,您会看到:

{
    "age": 94,
    "id": 14,
    "name": "tom",
    "status": "finished"
}