Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gevent
Commits
a9e7456b
Commit
a9e7456b
authored
Apr 11, 2012
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ThreadPool: make it possible to set 'size' property
parent
011f719f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
18 deletions
+33
-18
gevent/threadpool.py
gevent/threadpool.py
+33
-18
No files found.
gevent/threadpool.py
View file @
a9e7456b
...
...
@@ -49,10 +49,27 @@ class ThreadPool(object):
def
__len__
(
self
):
return
self
.
task_queue
.
unfinished_tasks
@
property
def
size
(
self
):
def
_get_size
(
self
):
return
self
.
_size
def
_set_size
(
self
,
size
):
if
size
<
0
:
raise
ValueError
(
'Size of the pool cannot be negative: %r'
%
(
size
,
))
if
size
>
self
.
_maxsize
:
raise
ValueError
(
'Size of the pool cannot be bigger than maxsize: %r > %r'
%
(
size
,
self
.
_maxsize
))
if
self
.
manager
:
self
.
manager
.
kill
()
while
self
.
_size
<
size
:
self
.
_add_thread
()
delay
=
0.0001
while
self
.
_size
>
size
:
while
self
.
_size
-
size
>
self
.
task_queue
.
unfinished_tasks
:
self
.
task_queue
.
put
(
None
)
sleep
(
delay
)
delay
=
min
(
delay
*
2
,
.
05
)
size
=
property
(
_get_size
,
_set_size
)
def
_init
(
self
,
maxsize
):
self
.
_size
=
0
self
.
_semaphore
=
Semaphore
(
1
)
...
...
@@ -82,37 +99,35 @@ class ThreadPool(object):
def
kill
(
self
):
if
self
.
manager
:
self
.
manager
.
kill
()
self
.
_manage
(
0
)
self
.
size
=
0
def
_adjust
(
self
,
maxsize
):
if
maxsize
is
None
:
maxsize
=
self
.
_maxsize
while
self
.
task_queue
.
unfinished_tasks
>
self
.
_size
and
self
.
_size
<
maxsize
:
def
_adjust_step
(
self
):
# if there is a possibility & necessity for adding a thread, do it
while
self
.
_size
<
self
.
_maxsize
and
self
.
task_queue
.
unfinished_tasks
>
self
.
_size
:
self
.
_add_thread
()
while
self
.
_size
-
maxsize
>
self
.
task_queue
.
unfinished_tasks
:
# while the number of threads is more than maxsize, kill one
# we do not check what's already in task_queue - it could be all Nones
while
self
.
_size
-
self
.
_maxsize
>
self
.
task_queue
.
unfinished_tasks
:
self
.
task_queue
.
put
(
None
)
if
self
.
_size
:
self
.
fork_watcher
.
start
(
self
.
_on_fork
)
else
:
self
.
fork_watcher
.
stop
()
def
_manage
(
self
,
maxsize
=
None
):
if
maxsize
is
None
:
maxsize
=
self
.
_maxsize
def
_adjust_wait
(
self
):
delay
=
0.0001
while
True
:
self
.
_adjust
(
maxsize
)
if
self
.
_size
<=
maxsize
:
self
.
_adjust
_step
(
)
if
self
.
_size
<=
self
.
_
maxsize
:
return
sleep
(
delay
)
delay
=
min
(
delay
*
2
,
.
05
)
def
adjust
(
self
):
if
self
.
manager
:
return
if
self
.
_adjust
(
self
.
maxsize
):
return
self
.
manager
=
Greenlet
.
spawn
(
self
.
_manage
)
self
.
_adjust_step
()
if
not
self
.
manager
and
self
.
_size
>
self
.
_maxsize
:
# might need to feed more Nones into the pool
self
.
manager
=
Greenlet
.
spawn
(
self
.
_adjust_wait
)
def
_add_thread
(
self
):
with
self
.
_lock
:
...
...
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