1

I'm trying to write to a global object from a flask request. I know that it's not recommended to use global variables in such scenario but I'm changing a state to perform some tests. I don't want to use any cache-memory solution such as pickledb.

I tried multiple approaches but none of them succeeded. Also I am trying to understand why the singleton solution does not work, eg

class Singleton:
    foo = 'bar'
    ...

@app.RestApi.service.route('/test/endpoint1')
def endpoint1():
    singleton_instance = Singleton.get_instance()
    singleton_instance.foo='not bar'
    print("Endpoint1 was called.")

At some point I call endpoint1: s = Singleton.get_instance() s.foo='bar'

#CALL ENDPOINT1 HERE USING URLLIB ....

time.sleep(10) #enough time to sync !!

And now if i get s.foo it won't be 'not bar'. instead it will not have changed !

Any idea why ??

Thank you

davidism
  • 121,510
  • 29
  • 395
  • 339
Ze Diogo
  • 11
  • 1

1 Answers1

0

Update: I was using Processes instead of Threads somewhere in my code without noticing. The context was being forked so that flask was running in a different process that the python instance where I was getting that output.

Ze Diogo
  • 11
  • 1