MongoDB database not updating from ThreadPoolExecutor background thread

Viewed 103

I'm creating a Flask app that does some calculations on GitHub repository statistics, storing results to a MongoDB database. I'm trying to implement the calculations and add the result to MongoDB in a background thread using ThreadPoolExecutor, but the database is not updated when submitting a new repository (in the addRepo method).

Yet, I receive no error logs during this process, so presumably the update_one method silently fails. When putting a print statement before and after the update_one line, the one before executes, but the one after does not.

Updating the database without using background threads works fine.

Code for app.py:

from flask import Flask, render_template, url_for, request, session, redirect
from flask_pymongo import PyMongo
from repoAnalysis import GitAnalysis
from concurrent.futures import ThreadPoolExecutor

app = Flask(__name__)
myGitAnalysis = GitAnalysis()

app.config["MONGO_DBNAME"] = "my-db-name"
app.config["MONGO_URI"] = "my-db-uri"

mongo = PyMongo(app)
executor = ThreadPoolExecutor(5)

#code here that logs in user, stores username in session as 'username'

#adds analysis of a repo to user on mongodb
@app.route('/<username>/repositories')
def changeRepos(username):
  return render_template('changeRepos.html')

@app.route('/<username>/repositories', methods=['POST'])
def changeReposPOST(username):
  token = request.form['token']
  repoName = request.form['reponame']
  executor.submit(addRepo, repoName, token)
  return redirect(url_for('checkStatus', username=username))

def addRepo(repoName, token):
  users = mongo.db.users
  #some magic that calculates some stuff and stores it in a dict in myGitAnalysis object
  data = {"repoName": repoName, "pickledDict": myGitAnalysis.pickledDict()}
  users.update_one({"username": session['username']}, {"$push": {"dicts": data}})

@app.route('/<username>/repositories/status')
def checkStatus(username):
  percent = myGitAnalysis.percentComplete()
  return str(percent)

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

Code for changeRepos.html (if that's necessary):

<!doctype html>

<head><title>Change Repositories</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css')}}"> 
    <meta charset=utf-8> </head>
    <body>
        <h3>Add a Repository</h3>

        <form  method="POST">

        <label for="reponame">Repository Name</label>
        <input type="text" name="reponame" required>

        <label for="token">GitHub Access Token</label>
        <input type="password" name="token" required>

        <input type="submit" value="Submit">
        
        </form>
    </body>

My best guess is an issue with the background threading, but I'm not sure. If anybody could offer any explanation to this issue, that would be appreciated.

0 Answers
Related