rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

reticulate::import_from_path() causing R session to abort on close of running plumber api #841

Open Ada-Nick opened 2 years ago

Ada-Nick commented 2 years ago

System details

- Session info ---------------------------------------------------------------
 setting  value                       
 version  R version 4.1.0 (2021-05-18)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_United Kingdom.1252 
 ctype    English_United Kingdom.1252 
 tz       Europe/London               
 date     2021-11-09  

Example application or steps to reproduce the problem

A plumber api I'm working on is dependent on a python module that we're developing, which is in a sub folder of the project directory:

.
├── plumber.R (define endpoints using python_module functions)
├── start.R (start plumber api defined in plumber.R)
├── global.R (import_from_path(module = 'python_module', path = '.'))
├── setup.py (define python module)
├── python_module/
│   ├── function1.py
│   └── function2.py

Within start.R I am sourcing global.R which loads a number of R packages and uses the reticulate import_from_path() function to load the python module.

The plumber process is started within start.R :

source('global.R')
pr("plumber.R") %>%
    pr_run(port = 80, host = "0.0.0.0")

Everything is working as expected. But when I stop the plumber process by hitting ESC my R session is aborted with a fatal error and causing me to start a new R session.

I am using the latest reticulate and plumber packages and R studio 1.4.1103

EDIT: A more reproducible example, difficult as we need an entire repo:

global.R

library(plumber)
library(reticulate)
module = import_from_path('python_module',  '.')

start.R

source('global.R')
pr("plumber.R") %>%
    pr_run(port = 80, host = "0.0.0.0")

plumber.R

#* @apiTitle example api

#* example endpoint
#* @param message A message
#* @get /example_endpoint
function(message){
    output = module$show_message(message)
    return(output)
}

setup.py

import setuptools
setuptools.setup(name='example_module',
version='0.1',
description='my package',
url='#',
author='nickbarton',
install_requires=['numpy==1.21.4', 'pandas', 'requests', 'matplotlib', 'boto3', 'scipy'],
author_email='',
packages=setuptools.find_packages(),
zip_safe=False)

python_module/test_function.py


def show_message(message):
      return message

init.py

from .test_function import *
meztez commented 2 years ago

@Ada-Nick Would you be able to create a small piece of code that reproduce the error?

Ada-Nick commented 2 years ago

A bit difficult as we need a python module but I have updated the response with an extended example

Ada-Nick commented 2 years ago

Looking into it a bit further, it seems that the crash only occurs when importing a sub python module within the python_module/test_function.py file such as:

import scipy.interpolate
def show_message(message):
      return message
schloerke commented 2 years ago

@Ada-Nick Could you run source("start.R") from within the terminal tab of the IDE? (Or even the Terminal application.)

I'm trying to see if plumber is causing R to quit. Or if the IDE is having issues. Thank you!

Ada-Nick commented 2 years ago

@schloerke Do you mean in the R studio console? Doing doesn't give any information, the plumber api start up as normal and the termination happens on close.

schloerke commented 2 years ago

@Ada-Nick I'm referring to the Terminal panel. See https://support.rstudio.com/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal-in-the-RStudio-IDE#send .

The IDE is tightly integrated with the R eco system. By running the code in the Terminal panel, those connections are at a minimum. (Where as the IDE knows a lot about the regular R console.)

mskyttner commented 2 years ago

@Ada-Nick I tried using these files ... slightly modified from your setup for example with regards to the call to "import_from_path". Using a plumber container to run it (no RStudio) seems to work, hopefully reproducibly, when using the docker-compose.yml in this repo.

Ada-Nick commented 2 years ago

@mskyttner Hi, sorry I had forgotten about this. This is the solution I went with as well. Thanks for your work on the plumber package!