-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1641 from rstudio/allow-background-python-threads
Allow Python backgrounds threads to run in parallel with R
- Loading branch information
Showing
12 changed files
with
244 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
|
||
py_allow_threads <- function(allow = TRUE) { | ||
if (allow) { | ||
reticulate_ns <- environment(sys.function()) | ||
for (f in sys.frames()) { | ||
if (identical(parent.env(f), reticulate_ns) && | ||
!identical(f, environment())) | ||
# Can't release the gil as unlocked while we're holding it | ||
# elsewhere on the callstack. | ||
stop("Python threads can only be unblocked from a top-level reticulate call") | ||
} | ||
} | ||
|
||
if (!was_python_initialized_by_reticulate()) | ||
stop("Can't safely unblock threads when R is running embedded") | ||
|
||
invisible(py_allow_threads_impl(allow)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import sys | ||
import os | ||
|
||
|
||
def run_file(path): | ||
with open(path, "r") as file: | ||
file_content = file.read() | ||
|
||
d = sys.modules["__main__"].__dict__ | ||
|
||
exec(file_content, d, d) | ||
|
||
|
||
class RunMainScriptContext: | ||
def __init__(self, path, args): | ||
self.path = path | ||
self.args = tuple(args) | ||
|
||
def __enter__(self): | ||
sys.path.insert(0, os.path.dirname(self.path)) | ||
|
||
self._orig_sys_argv = sys.argv | ||
sys.argv = [self.path] + list(self.args) | ||
|
||
def __exit__(self, *_): | ||
# try restore sys.path | ||
try: | ||
sys.path.remove(os.path.dirname(self.path)) | ||
except ValueError: | ||
pass | ||
# restore sys.argv if it's been unmodified | ||
# otherwise, leave it as-is. | ||
set_argv = [self.path] + list(self.args) | ||
if sys.argv == set_argv: | ||
sys.argv = self._orig_sys_argv | ||
|
||
|
||
def _run_file_on_thread(path, args=None): | ||
|
||
import _thread | ||
|
||
_thread.start_new_thread(run_file, (path, )) | ||
|
||
|
||
def _launch_lsp_server_on_thread(path, args): | ||
# for now, leave sys.argv and sys.path permanently modified. | ||
# Later, revisit if it's desirable/safe to restore after the initial | ||
# lsp event loop startup. | ||
RunMainScriptContext(path, args).__enter__() | ||
_run_file_on_thread(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.