Skip to content

ReaderT pattern instead of global mutable object? / multiple python instances? #34

@Eiko-Tokura

Description

@Eiko-Tokura

Hi Shimuuar,

I'm wondering about the following:

Is it possible that we create a PyEnv or a readerT-like scoped monad enviroment (ReaderT PyEnv IO a), instead of implicitly accesing a global mutable object over IO?

data PyEnv = PyEnv
  { pyState    :: TVar PyState
  , pyLock     :: TVar PyLock
  , pyEvalQ    :: Maybe (MVar EvalReq) 
  , pyGcChan   :: Maybe (Chan (Ptr PyObject))
  , pyMainTid  :: Maybe ThreadId
  , pyGcTid    :: Maybe ThreadId
  , pyInterpTS :: Ptr PyThreadState
  }

newtype Py a = Py { unPy :: PyEnv -> IO a } -- or ReaderT PyEnv IO a

withPythonEnv :: (PyEnv -> IO a) -> IO a
withPythonEnv k = bracket newInterpreter closeInterpreter k

runPyWith :: PyEnv -> Py a -> IO a
runPyWith env (Py m) = runReaderT m env

Also I would like to know, what are the essential difficulties that blocks the possibility to have multiple threads running (potentially) multiple python instances?

I'm not familiar with the codebase or difficulties that arises from it (so, sorry if I'm requesting something silly here!), but I'm curioius and I think it would be very useful (for many applications), if we could create multiple PyEnv (for multiple threads), we could potentially be running several python instances, each within a different scope?

Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions