determinate if app object is the server or created from a console script

6 views
Skip to first unread message

Gerhard Schmidt

unread,
Oct 13, 2023, 1:00:13 AM10/13/23
to pylons-discuss
Hi there,

I'm often running into the problem to determinate if the current app is
running as a server to server http requests or created from a console
script to do specific things.

Mostly it's to decide if a background thread should be started (needed
in the Server) or not (can cause problems in the console script) in the
ApplicationCreated event.

I'm using pyramid.paster.bootstrap to set up the app for a console script.

The problem is, the ApplicationCreated is called before I have a chance
to set anything in the registry to pass information along.

I there a clean way know from the app object passed to the event alone.
I tried to pass a custom Request to bootstrap but the request is not
passed to this event.

Regards
Estartu





OpenPGP_0x3EE6A5DC78826E6B.asc
OpenPGP_signature.asc

Thierry Florac

unread,
Oct 13, 2023, 2:19:01 AM10/13/23
to pylons-...@googlegroups.com
Hi Gerhard,

Maybe it's not the best way to handle this, but I generally create custom command-line scripts (using Buildout) and just check for sys.argv...

Best regards,
Thierry


--
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/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de.

Gerhard Schmidt

unread,
Oct 13, 2023, 3:47:15 AM10/13/23
to pylons-...@googlegroups.com
Hi Thierry,

not really a good option. The background thread is not started from the
main project, but from a additional package (used in multiple projects).
So it has no knowledge about the names of the console scripts.

I could check for pserver as command to determinate if it's the server,
but changing the wsgi server would break this.

Regards
Estartu

Am 13.10.23 um 08:18 schrieb Thierry Florac:
> Hi Gerhard,
>
> Maybe it's not the best way to handle this, but I generally create
> custom command-line scripts (using Buildout) and just check for sys.argv...
>
> Best regards,
> Thierry
> --
> https://www.ulthar.net <https://www.ulthar.net> --
> http://pyams.readthedocs.io <http://pyams.readthedocs.io>
>
>
> Le ven. 13 oct. 2023 à 07:00, Gerhard Schmidt <est...@augusta.de
> <mailto:est...@augusta.de>> a écrit :
>
> Hi there,
>
> I'm often running into the problem to determinate if the current app is
> running as a server to server http requests or created from a console
> script to do specific things.
>
> Mostly it's to decide if a background thread should be started (needed
> in the Server) or not (can cause problems in the console script) in the
> ApplicationCreated event.
>
> I'm using pyramid.paster.bootstrap to set up the app for a console
> script.
>
> The problem is, the ApplicationCreated is called before I have a chance
> to set anything in the registry to pass information along.
>
> I there a clean way know from the app object passed to the event alone.
> I tried to pass a custom Request to bootstrap but the request is not
> passed to this event.
>
> Regards
>      Estartu
>
>
>
>
>
> --
> 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
> <mailto:pylons-discuss%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de <https://groups.google.com/d/msgid/pylons-discuss/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de>.
>
> --
> 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
> <mailto:pylons-discus...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWDw%2BMWKioj5F_OBpeeBSNJAS3JAoU8DZmNsZ3hRX-x%3DAg%40mail.gmail.com <https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWDw%2BMWKioj5F_OBpeeBSNJAS3JAoU8DZmNsZ3hRX-x%3DAg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
OpenPGP_0x3EE6A5DC78826E6B.asc
OpenPGP_signature.asc

Thierry Florac

unread,
Oct 13, 2023, 3:53:30 AM10/13/23
to pylons-...@googlegroups.com
Don't you have any way to declare a variable into your process environment?

Regards,
Thierry
-- 
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/971cf4de-7235-47af-a92e-5da154230b18%40augusta.de.

Gerhard Schmidt

unread,
Oct 13, 2023, 4:52:14 AM10/13/23
to pylons-...@googlegroups.com
That's how I do it right now. But you have to remember it in every
script. As I said. This package is use in many projects, so a detection
without help from the script itself would be better.

Regards
Estartu

Am 13.10.23 um 09:53 schrieb Thierry Florac:
> Don't you have any way to declare a variable into your process environment?
>
> Regards,
> Thierry
> --
> Le ven. 13 oct. 2023 à 09:47, Gerhard Schmidt <est...@augusta.de
> <mailto:est...@augusta.de>> a écrit :
>
> Hi Thierry,
>
> not really a good option. The background thread is not started from the
> main project, but from a additional package (used in multiple
> projects).
> So it has no knowledge about the names of the console scripts.
>
> I could check for pserver as command to determinate if it's the server,
> but changing the wsgi server would break this.
>
> Regards
>     Estartu
>
> Am 13.10.23 um 08:18 schrieb Thierry Florac:
> > Hi Gerhard,
> >
> > Maybe it's not the best way to handle this, but I generally create
> > custom command-line scripts (using Buildout) and just check for
> sys.argv...
> >
> > Best regards,
> > Thierry
> > --
> > https://www.ulthar.net <https://www.ulthar.net>
> <https://www.ulthar.net <https://www.ulthar.net>> --
> > http://pyams.readthedocs.io <http://pyams.readthedocs.io>
> <http://pyams.readthedocs.io <http://pyams.readthedocs.io>>
> >
> >
> > Le ven. 13 oct. 2023 à 07:00, Gerhard Schmidt <est...@augusta.de
> <mailto:est...@augusta.de>
> > <mailto:est...@augusta.de <mailto:est...@augusta.de>>> a écrit :
> >     <mailto:pylons-discuss%2Bunsu...@googlegroups.com
> <mailto:pylons-discuss%252Buns...@googlegroups.com>>.
> >     To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/pylons-discuss/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de <https://groups.google.com/d/msgid/pylons-discuss/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de> <https://groups.google.com/d/msgid/pylons-discuss/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de <https://groups.google.com/d/msgid/pylons-discuss/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de>>.
> >
> > --
> > 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
> <mailto:pylons-discuss%2Bunsu...@googlegroups.com>
> > <mailto:pylons-discus...@googlegroups.com
> <mailto:pylons-discuss%2Bunsu...@googlegroups.com>>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWDw%2BMWKioj5F_OBpeeBSNJAS3JAoU8DZmNsZ3hRX-x%3DAg%40mail.gmail.com <https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWDw%2BMWKioj5F_OBpeeBSNJAS3JAoU8DZmNsZ3hRX-x%3DAg%40mail.gmail.com> <https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWDw%2BMWKioj5F_OBpeeBSNJAS3JAoU8DZmNsZ3hRX-x%3DAg%40mail.gmail.com?utm_medium=email&utm_source=footer <https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWDw%2BMWKioj5F_OBpeeBSNJAS3JAoU8DZmNsZ3hRX-x%3DAg%40mail.gmail.com?utm_medium=email&utm_source=footer>>.
>
> --
> 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
> <mailto:pylons-discuss%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/971cf4de-7235-47af-a92e-5da154230b18%40augusta.de <https://groups.google.com/d/msgid/pylons-discuss/971cf4de-7235-47af-a92e-5da154230b18%40augusta.de>.
>
> --
> 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
> <mailto:pylons-discus...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWBKBWA3VeLZZ6j1PjNj4zZnQjp0dHRqdQv1TgbA1tOJ2A%40mail.gmail.com <https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWBKBWA3VeLZZ6j1PjNj4zZnQjp0dHRqdQv1TgbA1tOJ2A%40mail.gmail.com?utm_medium=email&utm_source=footer>.
OpenPGP_0x3EE6A5DC78826E6B.asc
OpenPGP_signature.asc

Mike Orr

unread,
Oct 13, 2023, 2:32:59 PM10/13/23
to pylons-...@googlegroups.com
Maybe there's a variable in the WSGI environment that a real server
sets but an emulating script doesn't, or has a value that can be
distinguished as "real". For instance, a real request will have the
user agent set to a long string of version numbers and
backward-compatibility identifiers, while an emulated request may not
bother setting it or may set it to something simple like "wsgi" or
"python".
> --
> 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/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de.



--
Mike Orr <slugg...@gmail.com>

Mike Orr

unread,
Oct 13, 2023, 2:37:53 PM10/13/23
to pylons-...@googlegroups.com
How about a custom setting in the config file? If you're using
pyramid.paster.bootstrap you must be using a config file, and you can
have all scripts use the same file, different from the file the
webserver instance is using.
> 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/3632fc7e-6cfb-4991-a189-d4704d3d6ee2%40augusta.de.



--
Mike Orr <slugg...@gmail.com>

Gerhard Schmidt

unread,
Oct 14, 2023, 5:14:15 AM10/14/23
to pylons-...@googlegroups.com
The ApplicationCreated doesn't get a request object. So no luck there.

Regards
Estartu

Am 13.10.23 um 20:32 schrieb Mike Orr:
OpenPGP_0x3EE6A5DC78826E6B.asc
OpenPGP_signature.asc

Thierry Florac

unread,
Oct 14, 2023, 7:42:51 AM10/14/23
to pylons-...@googlegroups.com
Maybe I'm wrong, but I think that you can get access to the "global" registry settings without the need of getting a request, no?

Regards,
Thierry


Gerhard Schmidt

unread,
Oct 16, 2023, 2:42:16 PM10/16/23
to pylons-...@googlegroups.com
Yes. But I have no chance to write something there. Because the event is
called before the bootstrap call returns. The request object can be
passed to the call but really little else.

Regards
Estartu

Am 14.10.23 um 13:42 schrieb Thierry Florac:
> Maybe I'm wrong, but I think that you can get access to the "global"
> registry settings without the need of getting a request, no?
>
> Regards,
> Thierry
> --
> https://www.ulthar.net <https://www.ulthar.net> --
> http://pyams.readthedocs.io <http://pyams.readthedocs.io>
>
>
> Le sam. 14 oct. 2023 à 11:14, Gerhard Schmidt <est...@augusta.de
> <mailto:est...@augusta.de>> a écrit :
> <mailto:pylons-discuss%2Bunsu...@googlegroups.com>.
> >> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de <https://groups.google.com/d/msgid/pylons-discuss/008829c9-1180-4365-9f69-d22d59a09ccd%40augusta.de>.
> >
> >
> >
>
> --
> 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
> <mailto:pylons-discuss%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/1a530488-65ba-4e45-9f55-c5a88258d268%40augusta.de <https://groups.google.com/d/msgid/pylons-discuss/1a530488-65ba-4e45-9f55-c5a88258d268%40augusta.de>.
>
> --
> 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
> <mailto:pylons-discus...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWBHrkNrLc-LhSx9prW%2BjAdhu2maS33bqZmzQWWyQ_ofXg%40mail.gmail.com <https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWBHrkNrLc-LhSx9prW%2BjAdhu2maS33bqZmzQWWyQ_ofXg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
OpenPGP_0x3EE6A5DC78826E6B.asc
OpenPGP_signature.asc

Gerhard Schmidt

unread,
Oct 16, 2023, 2:44:16 PM10/16/23
to pylons-...@googlegroups.com
The config file used is controlled be the environment, in my case
docker/kubernets. So scripts and server must use the same to have the
same settings.

Regards
Estartu

Am 13.10.23 um 20:37 schrieb Mike Orr:
OpenPGP_0x3EE6A5DC78826E6B.asc
OpenPGP_signature.asc

Arndt Droullier

unread,
Oct 18, 2023, 10:19:33 AM10/18/23
to pylons-...@googlegroups.com
If you have access to the request object you can check req.environ to find out what is going. E.g. REMOTE_HOST or SERVER_NAME are  different if you are using a proxy server or bin/prequest via commandline. Details depend on your setup.

In case you don't have the request object you can use "get_current_request()" to acces it. (usr https://docs.pylonsproject.org/projects/pyramid/en/latest/api/threadlocal.html#pyramid.threadlocal.get_current_request)

Arndt.

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/0d9bcd7d-cf49-44ac-9245-b237a48475cc%40augusta.de.


--
----------------------------------------------------------------------------------------------
Arndt Droullier
Tel 0221-2725782
Nive GmbH, Händelstr. 26, Köln, Germany

Arndt Droullier

unread,
Oct 18, 2023, 11:34:33 AM10/18/23
to pylons-...@googlegroups.com
Sorry for the confusion. I was mislead. My previous mail does not refer to the Pyrmid ApplicationCreated Event, but a custom one. This won't work.

Arndt.

Arndt Droullier

unread,
Oct 18, 2023, 1:40:17 PM10/18/23
to pylons-...@googlegroups.com
Actually I had a similar problem some time ago and used a workaround since. 

I looked at the pyramid code and I don't think there is a way to handle this without setting up multiple configurations or startup scripts.
Though in my case it only needs a simple pyramid code change to come to a clean solution. 
I added a feature request to pyramid on gihub. Please have a look. Maybe it helps.


Basically the pyramid main function is called with additional info how the application was started (pserve, prequest, pshell).
I'm not sure if this helps in the context of ApplicationCreated but maybe you can move your code to the main() function.

Arndt.
Reply all
Reply to author
Forward
0 new messages