-1

I have a (beginner) question.

I intend to run more than one webapp (pyramid web apps) and I have a common library (let's call that base app) that may be used by both webapps and includes pyramid configuration includes etc. These webapps will eventually be separate wsgi scripts (probably) ending up sitting in the same virtualenv.

My question is: What is the isolation level in python if I monkey patch classes in this library (I am currently dynamically changing bases for some of the classes in that library that may be referenced from both webapps).

for example:

in base app:

class_from_baseapp(grandparent):
    pass

in derived app 2:

from baseapp import class_from_baseapp
#do some stuff with this class
#and have another bunch of child classes too!
class_from_childapp2(class_from_baseapp):
    pass

in derived app 1:

from baseapp import class_from_baseapp
# then what I do is I change this dynamically to
# class_from_baseapp(grandparent, mixin_class): 
# by altering the class' __bases__


class_from_childapp1(class_from_baseapp):
    pass

So again, my question is: Will this monkey patching leak to the other web app (web app 1), if it imports/uses the same class as above? I don't know how process and thread isolation in python interpreters work.

Ahmed
  • 93
  • 1
  • 5

1 Answers1

1

If you are running separate processes, the isolation is complete between processes. Each Python interpreter has its own references to the classes. Monkey-patching in-memory modules will not affect any process but the process the patching was done in.

Threads running in the same Python process all use the same classes. Depending on how the patching is done (and what references the threads get before the patching happens) the patch may not be seen by all threads, but generally it will be.

kindall
  • 178,883
  • 35
  • 278
  • 309