Page MenuHomePhabricator

Needing help running the webservice for a simple flask application
Closed, ResolvedPublic

Description

I was trying to create a simple flask web app at /data/project/whichsub but I can't run the webservice for it.

When I try to run it using lighttpd I get the following error in the error.log file:

2016-07-12 14:00:31: (log.c.166) server started 
2016-07-12 14:00:31: (mod_fastcgi.c.1103) the fastcgi-backend /data/project/whichsub/www/python/src/app.py failed to start: 
2016-07-12 14:00:31: (mod_fastcgi.c.1107) child exited with status 2 /data/project/whichsub/www/python/src/app.py 
2016-07-12 14:00:31: (mod_fastcgi.c.1110) If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.
If this is PHP on Gentoo, add 'fastcgi' to the USE flags. 
2016-07-12 14:00:31: (mod_fastcgi.c.1398) [ERROR]: spawning fcgi failed. 
2016-07-12 14:00:31: (server.c.1021) Configuration of plugins failed. Going down. 
[uWSGI] getting INI configuration from /data/project/whichsub/uwsgi.ini

My .lighttpd.conf file:

fastcgi.server += ( "/whichsub" =>
    ((
        "socket" => "/tmp/whichsub-fcgi.sock",
        "bin-path" => "/data/project/whichsub/www/python/src/app.py",
        "check-local" => "disable",
        "max-procs" => 1,
    ))
)

And when I try using the uwsgi-plain I get the following error in uwsgi.log:

.0-86-generic #131-Ubuntu SMP Thu May 12 23:33:13 UTC 2016
nodename: tools-webgrid-generic-1401
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /data/project/whichsub
detected binary path: /usr/bin/uwsgi-core
your processes number limit is 63706
your process address space limit is 4294967296 bytes (4096 MB)
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :40984 fd 3
uwsgi socket 1 bound to UNIX address /data/project/whichsub/whichsub.sock fd 4
Python version: 3.4.0 (default, Jun 19 2015, 14:24:19)  [GCC 4.8.2]
PEP 405 virtualenv detected: /data/project/whichsub/www/python/venv
Set PythonHome to /data/project/whichsub/www/python/venv
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1b1e850
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 363960 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./app.py", line 8, in <module>
    from typing import List
ImportError: No module named 'typing'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 29044)
spawned uWSGI worker 1 (pid: 29048, cores: 1)
spawned uWSGI worker 2 (pid: 29049, cores: 1)
spawned uWSGI worker 3 (pid: 29050, cores: 1)
spawned uWSGI worker 4 (pid: 29051, cores: 1)

As the error indicates, my app is being run by python 3.4.0, but I need it to run with my virtual environment python which is 3.5.2.

My uwsgi.ini file:

[uwsgi]
plugin = python3
socket = /data/project/whichsub/whichsub.sock
chdir = /data/project/whichsub/www/python/src
venv = /data/project/whichsub/www/python/venv
virtualenv = /data/project/whichsub/www/python/venv
module = app
callable = app
manage-script-name = true

My python app:

#!/data/project/whichsub/www/python/venv/bin/python
# -*- coding: utf-8 -*-

"""Find which subtemplates contain the given text."""


from os import name as os_name

from flask import Flask
from flask import request
from flask import render_template
import pywikibot as pwb
if os_name == 'posix':
    from flup.server.fcgi import WSGIServer


app = Flask(__name__)


def find_sub_templates(lookingfor: str, page: pwb.Page):
    found_templates = []
    if lookingfor in page.text:
        found_templates.append(template)
    for sub_template in page.templates(content=True):
        if lookingfor in sub_template.text:
            found_templates.append(sub_template)
    return found_templates


@app.route('/' if os_name != 'posix' else '/whichsub/')
def main():
    code = request.args.get('code', 'en')
    family = request.args.get('family', 'wikipedia')
    pagetitle = request.args.get('pagetitle', '')
    lookingfor = request.args.get('lookingfor', '')
    site = pwb.Site(code, family)
    try:
        page = pwb.Page(site, pagetitle)
        templates = find_sub_templates(lookingfor, page)
    except ValueError:
        templates = None
    return render_template(
        'main.html',
        code=code,
        family=family,
        pagetitle=pagetitle,
        lookingfor=lookingfor,
        templates=templates,
    )


if __name__ == '__main__':
    if os_name == 'posix':
        WSGIServer(app).run()
    else:
        app.run(debug=True)

I'm completely stuck at this point. Any help is appreciated. Feel free to ask any question.
If needed I would be happy to add you to the tool account...

Event Timeline

Dalba created this task.Jul 12 2016, 2:28 PM
Restricted Application added a project: Cloud-Services. · View Herald TranscriptJul 12 2016, 2:28 PM
Restricted Application added subscribers: Zppix, Aklapper. · View Herald Transcript

Do you *really* need 3.5? I think it'll be much easier for everyone if you could just use 3.4 - I don't think we can provide support for 3.5 yet unfortunately...

Dalba added a comment.Jul 12 2016, 3:10 PM

Not *really*... I'll try python3.4.

Just a dumb question: how do you create a virtual environment for python 3.4? Should I compile another python3.4 from the source? The following does not work obviously:

tools.whichsub@tools-bastion-03:~/www/python$ python3 -m venv venv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

@Dalba

tools.piagetbot@tools-bastion-03:~$ virtualenv ~/venv -p /usr/bin/python3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /data/project/piagetbot/venv/bin/python3
Also creating executable in /data/project/piagetbot/venv/bin/python
Installing setuptools, pip...done.

(of course replace the "~/venv" with the directory you want the virtualenv to go)

You can do so with:

virtualenv -p python3 venv
Dalba closed this task as Resolved.Jul 12 2016, 4:13 PM
Dalba claimed this task.

tom29739 and yuvipanda, thank you so much! I could get to work with plain-uwsgi.

Dalba updated the task description. (Show Details)Jul 12 2016, 4:16 PM