Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mitogen
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
mitogen
Commits
f4ba66e3
Commit
f4ba66e3
authored
Mar 22, 2018
by
David Wilson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
issue #155: allocate child IDs in batches of 1000.
Avoids a roundtrip for every fork.
parent
0c771070
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
8 deletions
+42
-8
mitogen/master.py
mitogen/master.py
+12
-3
mitogen/parent.py
mitogen/parent.py
+17
-4
tests/id_allocation_test.py
tests/id_allocation_test.py
+13
-1
No files found.
mitogen/master.py
View file @
f4ba66e3
...
...
@@ -724,16 +724,25 @@ class IdAllocator(object):
finally:
self.lock.release()
def allocate_block(self):
self.lock.acquire()
try:
id_ = self.next_id
self.next_id += 1000
return id_, id_ + 1000
finally:
self.lock.release()
def on_allocate_id(self, msg):
if msg == mitogen.core._DEAD:
return
id_
= self.allocate
()
id_
, last_id = self.allocate_block
()
requestee = self.router.context_by_id(msg.src_id)
allocated = self.router.context_by_id(id_, msg.src_id)
LOG.debug('
%
r
:
allocating
%
r
to
%
r', self, allocated, requestee)
msg.reply(
id_
)
LOG.debug('
%
r
:
allocating
[
%
r
..
%
r
)
to
%
r', self, allocated, requestee)
msg.reply(
(id_, last_id)
)
LOG.debug('
%
r
:
publishing
route
to
%
r
via
%
r', self,
allocated, requestee)
...
...
mitogen/parent.py
View file @
f4ba66e3
...
...
@@ -37,6 +37,7 @@ import socket
import
sys
import
termios
import
textwrap
import
threading
import
time
import
zlib
...
...
@@ -423,12 +424,24 @@ class Stream(mitogen.core.Stream):
class
ChildIdAllocator
(
object
):
def
__init__
(
self
,
router
):
self
.
router
=
router
self
.
lock
=
threading
.
Lock
()
self
.
it
=
iter
(
xrange
(
0
))
def
allocate
(
self
):
master
=
mitogen
.
core
.
Context
(
self
.
router
,
0
)
return
master
.
send_await
(
mitogen
.
core
.
Message
(
dst_id
=
0
,
handle
=
mitogen
.
core
.
ALLOCATE_ID
)
)
self
.
lock
.
acquire
()
try
:
for
id_
in
self
.
it
:
return
id_
master
=
mitogen
.
core
.
Context
(
self
.
router
,
0
)
start
,
end
=
master
.
send_await
(
mitogen
.
core
.
Message
(
dst_id
=
0
,
handle
=
mitogen
.
core
.
ALLOCATE_ID
)
)
self
.
it
=
iter
(
xrange
(
start
,
end
))
finally
:
self
.
lock
.
release
()
return
self
.
allocate
()
class
Router
(
mitogen
.
core
.
Router
):
...
...
tests/id_allocation_test.py
View file @
f4ba66e3
...
...
@@ -8,8 +8,20 @@ import id_allocation
class
SlaveTest
(
testlib
.
RouterMixin
,
testlib
.
TestCase
):
def
test_slave_allocates_id
(
self
):
context
=
self
.
router
.
local
()
# Master's allocator named the context 1.
self
.
assertEquals
(
1
,
context
.
context_id
)
# First call from slave allocates a block (2..1001)
id_
=
context
.
call
(
id_allocation
.
allocate_an_id
)
self
.
assertEqual
(
id_
,
2
)
# Second call from slave allocates from block (3..1001)
id_
=
context
.
call
(
id_allocation
.
allocate_an_id
)
self
.
assertEqual
(
id_
,
self
.
router
.
id_allocator
.
next_id
-
1
)
self
.
assertEqual
(
id_
,
3
)
# Subsequent master allocation does not collide
c2
=
self
.
router
.
local
()
self
.
assertEquals
(
1002
,
c2
.
context_id
)
if
__name__
==
'__main__'
:
...
...
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