Strange problem with request method

11 views
Skip to first unread message

Gerhard Schmidt

unread,
Oct 11, 2021, 4:35:00 AM10/11/21
to pylons-discuss
Hi,

today I encountered a very strange problem with request methods.

<--- code --->

cache = {'agents': set(),
'time': 0.0}
cache_lock = Lock()


def in_agent_cache(request):
with cache_lock:
return request.user_agent in cache['agents']

<--- end code --->

If I call the method directly, everything works fine.

if I register the method as a request method and call it via
request.in_agent_cache() the dict is always the default version, the set
empty and time .

If I move the dict and lock to another file and import it. Everything
works even as a request method.

The log messages are also recorded under a wrong module

2021-10-11 10:29:33,153 INFO [agent_cache:96][waitress-0]
in_agent_cache called

agent_cache is the name of the py file and not the package it's in.

Any hint if I'm doing something wrong.

Regards
Estartu






Michael Merickel

unread,
Oct 11, 2021, 12:33:42 PM10/11/21
to pylons-...@googlegroups.com
I think there's a lot of unknowns in helping unpack this - for starters there's no code supplied that actually modifies the cache, so of course it is empty!

- There's a few gotchas in python - for example if this is done in the main file in your app (foo.py) and you run "python -m foo" then the module may be imported twice as "__main__" and as "foo" which can cause surprises.

- I see you're using waitress so this probably isn't a problem, but you're using an in-memory cache and so nothing will be shared across processes if you ever fork.

- 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/90ae76af-8406-3ec5-b490-2f4af254431d%40augusta.de.

Mike Orr

unread,
Oct 11, 2021, 12:58:32 PM10/11/21
to pylons-...@googlegroups.com
Where is the cache stored, and which Lock class are you using?
Application globals are tricky in Pyramid. One place you can store
them is a registry attribute (request.registry.myattr); I use this for
SQLAlchemy engines. Or you could stuff it into the configuration
settings at startup. The registry has a native API for global objects
but it's kind of complicated; 'pyramid_services' makes it more
intuitive. But that may be overkill for one simple object.

If your cache lookup is in view code; I'd encapsulate it into a
request method, to decouple the views from the cache location.

Logging entries usually include the module name, so that's not
unusual. What's unusual is it's not fully qualified (
'myapp.lib.agent_cache'). You may have disabled displaying the parents
in the logging configuration, or more worrisome, it may be importing
it as a top-level import or switching between two different
'agent_cache' modules. You can print or log ``repr(agent_cache)`` to
see where it comes from and its parents.
> --
> 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/90ae76af-8406-3ec5-b490-2f4af254431d%40augusta.de.



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

Gerhard Schmidt

unread,
Oct 12, 2021, 12:35:28 AM10/12/21
to Michael Merickel, pylons-discuss
Hi Michael,

there is a code to fill the cache.

I spend 3 hours debugging this.

Importing and calling the method directly works perfectly well. If i
call it in the next line in code via request object the cache is empty.

The first call produces a log entry with the correct package. The call
via request object produces the log message in package

moving the cache object to another python file fixes the problem.

My guess is, request methods somehow reimport the module without the
correct package. So the cache get re inited and the package information
is lost.

Regards
Estartu

Am 11.10.21 um 18:33 schrieb Michael Merickel:

Gerhard Schmidt

unread,
Oct 12, 2021, 12:54:49 AM10/12/21
to pylons-...@googlegroups.com
Hi,

I think i fixed the problem.

The error was a wrong import when registering the request method. The
import worked within the ide envoirement but not outside and inside the
ide in porduced this strange behavior.

Regards
Estartu

Am 11.10.21 um 18:58 schrieb Mike Orr:
Reply all
Reply to author
Forward
0 new messages