Trying to execute console_scripts in web browser (with nginx and uwsgi)

33 views
Skip to first unread message

Emerson Barea

unread,
Feb 27, 2020, 6:41:21 PM2/27/20
to pylons-discuss
Hi there.

My application has some scripts on .app.scripts (.app.scripts.tests and .app.scripts.config).

I configured this scripts in setup.py file like this:

entry_points={
'paste.app_factory': [
'main = minisecbgp:main'
],
'console_scripts': [
'initialize_minisecbgp_db = minisecbgp.scripts.initialize_db:main',
'tests = minisecbgp.scripts.tests:main',
'validate_hostname = minisecbgp.scripts.validate_hostname:main',
'config = minisecbgp.scripts.config:main',
],

I call these two scripts in my view with the code below:

arguments = ['--config_file=minisecbgp.ini',
'--hostname=%s' % form.node.data,
'--username=%s' % form.username.data,
'--password=%s' % form.password.data]
subprocess.Popen(['config'] + arguments)

and, when I run my application with the commands  below, everything works well. The page works fine and the scripts tests and config works well.

pip install -e ".[testing]"
pserve minisecbgp.ini --reload

So, I want to put my app in production, and I'm trying to use uwsgi and nginx to do it. When I configured uwsgi and nginx and open the app in browser, the app works well, but when I call the view that executes the scripts tests and config, I receave a 502 bad gateway error. Looking at syslog file, I receaved this error:

Feb 27 20:12:56 lpttch uwsgi[14110]:     subprocess.Popen(['tests'] + arguments)
Feb 27 20:12:56 lpttch uwsgi[14110]:   File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
Feb 27 20:12:56 lpttch uwsgi[14110]:     restore_signals, start_new_session)
Feb 27 20:12:56 lpttch uwsgi[14110]:   File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
Feb 27 20:12:56 lpttch uwsgi[14110]:     raise child_exception_type(errno_num, err_msg, err_filename)
Feb 27 20:12:56 lpttch uwsgi[14110]: FileNotFoundError: [Errno 2] No such file or directory: 'tests': 'tests'

Please, can somebody help me?

Emerson

Michael Merickel

unread,
Feb 27, 2020, 7:28:04 PM2/27/20
to Pylons
Your environment isn't modifying the env PATH, which is what Popen is relying on to find the script.

It'd be better not rely on the PATH and instead just run the code using `python -m foo`, but that doesn't actually work with console scripts. You would instead do `subprocess.Popen([sys.executable, '-m', 'minisecbgp.scripts.config'] + arguments)`. You'd then need to define an `if __name__ == '__main__': main()` in your script instead of relying on the console script to invoke your main function.

Alternatively fix your PATH, but I find that less ideal because it can change per-environment where the console scripts are actually installed.

- Michael

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/9a78ebba-f50d-4143-9fea-550d3bce9e1e%40googlegroups.com.


--

Michael

Emerson Barea

unread,
Mar 3, 2020, 1:37:01 PM3/3/20
to pylons-discuss
Hi Michael, thanks for your reply. I'm sorry I didn't confirm it earlier, but I've been quite sick this past week.

Well, I tried to follow the suggestion you made, making the following changes:

scripts/tests.py

def main(argv):
<my code here>


if
__name__ == '__main__':
main(argv=sys.argv[1:])


my view code:

arguments = ['--config_file=minisecbgp.ini',
'--execution_type=create_node',
             '--hostname=%s' % form.node.data,
'--username=%s' % form.username.data,
'--password=%s' % form.password.data]
#subprocess.Popen(['tests'] + arguments)
subprocess.Popen([sys.executable, '-m', 'minisecbgp.scripts.tests'] + arguments)

and now I receive this erro when I try to execute the view in browser:

Mar  3 15:24:57 lpttch uwsgi[7284]: /home/tocha/Documentos/projetos/MiniSecBGP/venv/bin/uwsgi: unrecognized option '--config_file=minisecbgp.ini'
Mar  3 15:24:57 lpttch uwsgi[7284]: getopt_long() error

I tried to modify the setyp.py console_scripts to
'tests = minisecbgp.scripts.tests'
or removing this entry_point, since the call to the script is being made directly now, but I always got the same error.

Please, do you known what I'm doing wrong?

Thank you.

Emerson
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-...@googlegroups.com.


--

Michael

Michael Merickel

unread,
Mar 3, 2020, 1:40:42 PM3/3/20
to Pylons
I suspect it's something to do with UWsgi doing funky things with fork and subprocesses. For example https://stackoverflow.com/questions/17592692/running-a-subprocess-in-uwsgi-application.

To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/5cc6641f-ffe2-4b16-b26f-99edf01865f8%40googlegroups.com.


--

Michael

Emerson Barea

unread,
Mar 3, 2020, 2:39:32 PM3/3/20
to pylons-discuss
So I'll look for another python server than uwsgi to run my app.

Thank you Michael.

Michael Merickel

unread,
Mar 3, 2020, 2:45:42 PM3/3/20
to Pylons
Sure I'd at least see if things work fine using waitress to try and confirm that it's a uwsgi issue. I'm sure uwsgi can be fixed to deal with the subprocesses better but I do not know how myself.

To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/5a08d16d-22a2-4ed8-9438-6bb16230509a%40googlegroups.com.


--

Michael
Reply all
Reply to author
Forward
0 new messages