Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace run_in_thread decorator with run_in_process #7980

Closed
1 task
wochinge opened this issue Feb 17, 2021 · 3 comments
Closed
1 task

replace run_in_thread decorator with run_in_process #7980

wochinge opened this issue Feb 17, 2021 · 3 comments
Labels
area:rasa-oss 🎡 Anything related to the open source Rasa framework area:rasa-oss/server Issues focused running rasa as server (e.g. via `rasa run`) type:maintenance 🔧 Improvements to tooling, testing, deployments, infrastructure, code style.

Comments

@wochinge
Copy link
Contributor

wochinge commented Feb 17, 2021

Current Situation We use the[ run_in_thread decorator|

def run_in_thread(f: Callable[..., Coroutine]) -> Callable:
] to process blocking, computanional intense operations asynchronously and to avoid blocking the server.

Issue We recently had an issue where we found out that the pykwalify library was not used in a thread safe way. This was fixed but as @joejuzl pointed out:

Maybe it would be safer going forward to just stop using multithreading? We could easily run into more issues like this in the future unless we ensure the threadsafety of all the code we write, and all the libraries we use! We could use multiprocessing instead (should be a very easy change).

Implementation notes: While this might seem trivial, it isn't as the decorated functions are local functions and hence can't be pickled and also the Sanic request object can't be picked. You can reproduce the errors using:

def _run_in_process(f, request: Request, *args: Any, **kwargs: Any) -> None:

1\. This is a new thread, so we need to create and set a new event loop
 thread_loop = asyncio.new_event_loop()
 asyncio.set_event_loop(thread_loop)

 try:
 return thread_loop.run_until_complete(f(request, *args, **kwargs))
 finally:
 thread_loop.close()

def run_in_thread(f: Callable[..., Coroutine]) -> Callable:
 """Decorator which runs request on a separate thread.

 Some requests (e.g. training or cross-validation) are computional intense requests.
 This means that they will block the event loop and hence the processing of other
 requests. This decorator can be used to process these requests on a separate thread
 to avoid blocking the processing of incoming requests.

 Args:
 f: The request handler function which should be decorated.

 Returns:
 The decorated function.
 """

 @wraps(f)
 async def decorated_function(
 request: Request, *args: Any, **kwargs: Any
 ) -> HTTPResponse:
 with concurrent.futures.ProcessPoolExecutor() as pool:
 return await request.app.loop.run_in_executor(
 pool, functools.partial(_run_in_process, f, request, *args, **kwargs)
 )

 return decorated_function

Definition of Done

  • run_in_thread is replaced by run_in_process
@wochinge wochinge added type:maintenance 🔧 Improvements to tooling, testing, deployments, infrastructure, code style. area:rasa-oss 🎡 Anything related to the open source Rasa framework area:rasa-oss/server Issues focused running rasa as server (e.g. via `rasa run`) labels Feb 17, 2021
@stale
Copy link

stale bot commented Jul 21, 2021

Exalate commented:

stale[bot] commented:

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 21, 2021
@wochinge wochinge removed the stale label Jul 26, 2021
@stale
Copy link

stale bot commented Jan 9, 2022

Exalate commented:

stale[bot] commented:

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 9, 2022
@rasabot-exalate rasabot-exalate added area:rasa-oss and removed type:maintenance 🔧 Improvements to tooling, testing, deployments, infrastructure, code style. area:rasa-oss 🎡 Anything related to the open source Rasa framework labels Mar 15, 2022 — with Exalate Issue Sync
@stale stale bot removed the stale label Mar 15, 2022
@m-vdb m-vdb added type:maintenance 🔧 Improvements to tooling, testing, deployments, infrastructure, code style. area:rasa-oss 🎡 Anything related to the open source Rasa framework and removed type:maintenance labels Mar 16, 2022
@rasabot-exalate rasabot-exalate added area:rasa-oss :ferris wheel: stale and removed type:maintenance 🔧 Improvements to tooling, testing, deployments, infrastructure, code style. area:rasa-oss 🎡 Anything related to the open source Rasa framework labels Mar 17, 2022 — with Exalate Issue Sync
@stale stale bot removed the stale label Mar 17, 2022
@rasabot-exalate rasabot-exalate added area:rasa-oss 🎡 Anything related to the open source Rasa framework stale type:maintenance_:wrench: and removed type:maintenance labels Mar 17, 2022 — with Exalate Issue Sync
@stale stale bot removed the stale label Mar 17, 2022
@rasabot-exalate rasabot-exalate added type:maintenance 🔧 Improvements to tooling, testing, deployments, infrastructure, code style. and removed type:maintenance_:wrench: labels Mar 21, 2022 — with Exalate Issue Sync
@sync-by-unito
Copy link

sync-by-unito bot commented Dec 19, 2022

➤ Maxime Verger commented:

💡 Heads up! We're moving issues to Jira: https://rasa-open-source.atlassian.net/browse/OSS.

From now on, this Jira board is the place where you can browse (without an account) and create issues (you'll need a free Jira account for that). This GitHub issue has already been migrated to Jira and will be closed on January 9th, 2023. Do not forget to subscribe to the corresponding Jira issue!

➡️ More information in the forum: https://forum.rasa.com/t/migration-of-rasa-oss-issues-to-jira/56569.

@m-vdb m-vdb closed this as completed Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:rasa-oss 🎡 Anything related to the open source Rasa framework area:rasa-oss/server Issues focused running rasa as server (e.g. via `rasa run`) type:maintenance 🔧 Improvements to tooling, testing, deployments, infrastructure, code style.
Projects
None yet
Development

No branches or pull requests

3 participants