Skip to content
This repository has been archived by the owner on Sep 14, 2019. It is now read-only.

Commit

Permalink
Run subprocess future asynchronously.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarp committed Aug 6, 2015
1 parent 67d6263 commit 3a0a97d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 8 additions & 7 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import logging
import functools
import concurrent.futures

from invoke import task, run
Expand Down Expand Up @@ -42,7 +41,6 @@ def apify(file_name, table_name=None, primary_name='id', insert=True):
@task
def serve(host='0.0.0.0', port=5000, debug=False):
from sandman import app
from sandman.model import activate
app.json_encoder = utils.APIJSONEncoder
app.config['SERVER_PORT'] = port
app.config['SQLALCHEMY_DATABASE_URI'] = config.SQLA_URI
Expand All @@ -61,10 +59,13 @@ def protect_admin():
blueprint = aws.make_blueprint()
app.register_blueprint(blueprint)

# Load bucket in a separate process, then activate sandman in the main process
with concurrent.futures.ProcessPoolExecutor() as pool:
future = pool.submit(aws.fetch_bucket)
callback = functools.partial(activate, base=utils.ReadOnlyModel, browser=False)
future.add_done_callback(callback)
# Activate sandman with existing models
utils.activate(admin=True)

# Load bucket in a separate process, then re-activate sandman in the main process
pool = concurrent.futures.ProcessPoolExecutor()
future = pool.submit(aws.fetch_bucket)
future.add_done_callback(utils.activate)
pool.shutdown(wait=False)

app.run(host=host, port=port, debug=debug)
5 changes: 4 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import sqlalchemy as sa

from sandman import db
from sandman.model import activate
from sandman.model.models import Model
from sandman.model import activate as sandman_activate

import config

Expand All @@ -21,6 +21,9 @@ def default(self, o):
return o.isoformat()
return super(APIJSONEncoder, self).default(o)

def activate(base=ReadOnlyModel, browser=False, admin=False, **kwargs):
sandman_activate(base=base, browser=browser, admin=admin, **kwargs)

def drop_table(tablename, metadata=None, engine=None):
metadata = metadata or sa.MetaData()
engine = engine or sa.create_engine(config.SQLA_URI)
Expand Down

0 comments on commit 3a0a97d

Please sign in to comment.