Heroku-WebHook查询和功能

2024-03-28 14:15:14 发布

您现在位置:Python中文网/ 问答频道 /正文

使用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上构建一个服务器?你知道吗


Tags: the对象textimportself脚本json元素
1条回答
网友
1楼 · 发布于 2024-03-28 14:15:14

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"
}

相关问题 更多 >