1

I have the following chalice app:

# encoding: utf-8
from chalice import Chalice
from models import ContentItem

app = Chalice(app_name='moderations')
app.debug = True


@app.route("/item/{item_id}", methods=['GET'])
def view_item(item_id):
    instance = ContentItem.get(ContentItem.id == item_id)
    return model_to_dict(instance)

@app.route("/item/{item_id}", methods=['POST'])
def save_item(item_id):
    request = app.current_request
    data = request.json_body
    instance = ContentItem.get(ContentItem.id == item_id)
    instance.content = data
    instance.save()
    return


def model_to_dict(instance):
    data = {'content': instance.content, 'id': instance.id}
    return data

And I am running chalice local

When to go to GET http://localhost:8000/item/1234, I get the record fine But then if I go to POST http://localhost:8000/item/1234, nothing happens, just sits there waiting ...

If I post first, which will work fine, then get the detail page, which results the same waiting game.

What have I done wrong?

James Lin
  • 25,028
  • 36
  • 133
  • 233

1 Answers1

0

The connection is not closed properly. before_request and after_request not supported by chalice yet. So we need to close the connection as it finish.

I suggest to use ExecutionContext to create separate connecton for the each exection.

@app.route("/item/{item_id}", methods=['GET'])
def view_item(item_id):

    with db.execution_context():
        # This statement is executed using the new `ctx_conn`.
        instance = ContentItem.get(ContentItem.id == item_id)
    return model_to_dict(instance)
coleifer
  • 24,887
  • 6
  • 60
  • 75
Raja Simon
  • 10,126
  • 5
  • 43
  • 74