Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
news
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sartika Aritonang
news
Commits
bd72f999
Commit
bd72f999
authored
May 29, 2020
by
Sartika Aritonang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
e57f3032
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
0 deletions
+86
-0
current_thread_executor.py
stbi/Lib/site-packages/asgiref/current_thread_executor.py
+86
-0
No files found.
stbi/Lib/site-packages/asgiref/current_thread_executor.py
0 → 100644
View file @
bd72f999
import
queue
import
threading
import
time
from
concurrent.futures
import
Executor
,
Future
class
_WorkItem
(
object
):
"""
Represents an item needing to be run in the executor.
Copied from ThreadPoolExecutor (but it's private, so we're not going to rely on importing it)
"""
def
__init__
(
self
,
future
,
fn
,
args
,
kwargs
):
self
.
future
=
future
self
.
fn
=
fn
self
.
args
=
args
self
.
kwargs
=
kwargs
def
run
(
self
):
if
not
self
.
future
.
set_running_or_notify_cancel
():
return
try
:
result
=
self
.
fn
(
*
self
.
args
,
**
self
.
kwargs
)
except
BaseException
as
exc
:
self
.
future
.
set_exception
(
exc
)
# Break a reference cycle with the exception 'exc'
self
=
None
else
:
self
.
future
.
set_result
(
result
)
class
CurrentThreadExecutor
(
Executor
):
"""
An Executor that actually runs code in the thread it is instantiated in.
Passed to other threads running async code, so they can run sync code in
the thread they came from.
"""
def
__init__
(
self
):
self
.
_work_thread
=
threading
.
current_thread
()
self
.
_work_queue
=
queue
.
Queue
()
self
.
_broken
=
False
def
run_until_future
(
self
,
future
):
"""
Runs the code in the work queue until a result is available from the future.
Should be run from the thread the executor is initialised in.
"""
# Check we're in the right thread
if
threading
.
current_thread
()
!=
self
.
_work_thread
:
raise
RuntimeError
(
"You cannot run CurrentThreadExecutor from a different thread"
)
# Keep getting work items and checking the future
try
:
while
True
:
# Get a work item and run it
try
:
work_item
=
self
.
_work_queue
.
get
(
block
=
False
)
except
queue
.
Empty
:
# See if the future is done (we only exit if the work queue is empty)
if
future
.
done
():
return
# Prevent hot-looping on nothing
time
.
sleep
(
0.001
)
else
:
work_item
.
run
()
del
work_item
finally
:
self
.
_broken
=
True
def
submit
(
self
,
fn
,
*
args
,
**
kwargs
):
# Check they're not submitting from the same thread
if
threading
.
current_thread
()
==
self
.
_work_thread
:
raise
RuntimeError
(
"You cannot submit onto CurrentThreadExecutor from its own thread"
)
# Check they're not too late or the executor errored
if
self
.
_broken
:
raise
RuntimeError
(
"CurrentThreadExecutor already quit or is broken"
)
# Add to work queue
f
=
Future
()
work_item
=
_WorkItem
(
f
,
fn
,
args
,
kwargs
)
self
.
_work_queue
.
put
(
work_item
)
# Return the future
return
f
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment