本文整理汇总了Python中multiprocessing.Semaphore.release方法的典型用法代码示例。如果您正苦于以下问题:Python Semaphore.release方法的具体用法?Python Semaphore.release怎么用?Python Semaphore.release使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Semaphore
的用法示例。
在下文中一共展示了Semaphore.release方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getPeerID
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def getPeerID(self, peerId):
semaphore = Semaphore()
semaphore.acquire()
peer = self.__layout[peerId]
semaphore.release()
return peer
示例2: getNeighbors
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def getNeighbors(self, peer):
semaphore = Semaphore()
semaphore.acquire()
neighbors = self.__layout[peer.getId()].getNeighbors()
semaphore.release()
return neighbors
示例3: Msg
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
class Msg(object):
"""
TODO: Not documenting this class because it may go away.
"""
def __init__(self, size):
self.s_e = Semaphore(1)
self.s_f = Semaphore(0)
self.s_buf = Array(ct.c_ubyte, size)
def send(self, func):
self.s_e.acquire()
self.s_buf.acquire()
send_result = func(self.s_buf._obj)
self.s_buf.release()
self.s_f.release()
return send_result
def recv(self, func):
self.s_f.acquire()
self.s_buf.acquire()
recv_result = func(self.s_buf._obj)
self.s_buf.release()
self.s_e.release()
return recv_result
示例4: Msg
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
class Msg(object):
"""
Data structure encapsulating a message.
"""
def __init__(self, size):
self.s_e = Semaphore(1)
self.s_f = Semaphore(0)
self.s_buf = Array(ct.c_ubyte, size)
def send(self, func):
self.s_e.acquire()
self.s_buf.acquire()
send_result = func(self.s_buf._obj)
self.s_buf.release()
self.s_f.release()
return send_result
def recv(self, func):
self.s_f.acquire()
self.s_buf.acquire()
recv_result = func(self.s_buf._obj)
self.s_buf.release()
self.s_e.release()
return recv_result
示例5: setCurrentSimulationTime
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def setCurrentSimulationTime(self, currentSimulationTime):
semaphore = Semaphore()
semaphore.acquire()
self.__currentSimulationTime = currentSimulationTime
semaphore.release()
return self.__currentSimulationTime
示例6: countNeighbors
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def countNeighbors(self, peer):
semaphore = Semaphore()
semaphore.acquire()
count = peer.countNeighbors()
semaphore.release()
return count
示例7: getNeighborIt
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def getNeighborIt(self, peer):
semaphore = Semaphore()
semaphore.acquire()
neighbors = []
for neighbor in self.__layout[peer.getId()].getNeighbors():
neighbors.append(neighbor.getTargetPeer())
neighborIt = neighbors.__iter__()
semaphore.release()
return neighborIt
示例8: addPeer
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def addPeer(self, peer):
if self.__layout.has_key(peer.getPID()):
return False
semaphore = Semaphore()
semaphore.acquire()
self.__layout[peer.getPID()] = peer
semaphore.release()
NetworkLogger().resgiterLoggingInfo("Add peer %s in Layout Network "%(peer.getPID()))
return self.__layout.has_key(peer.getPID())
示例9: __init__
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
class Barrier:
def __init__(self, n):
self.n = n
self.counter = SharedCounter(0)
self.barrier = Semaphore(0)
def wait(self):
with self.counter.lock:
self.counter.val.value += 1
if self.counter.val.value == self.n:
self.barrier.release()
self.barrier.acquire()
self.barrier.release()
示例10: WorkQueue
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
class WorkQueue(object):
def __init__(self):
self.request_rfd, self.request_wfd = os.pipe()
self.response_rfd, self.response_wfd = os.pipe()
self.response_reader = ResponseReader(self.response_rfd)
self.request_sem = Semaphore()
self.response_sem = Semaphore()
def submit_request(self, id, address, head, body):
try:
ip_str, port = address
ipa, ipb, ipc, ipd = map(int, ip_str.split("."))
except:
port = ipa = ipb = ipc = ipd = 0
os.write(
self.request_wfd, REQUEST_HEADER.pack(id, ipa, ipb, ipc, ipd, port, len(head), len(body)) + head + body
)
def get_request(self):
self.request_sem.acquire()
header = ""
bytes_to_read = REQUEST_HEADER.size
while bytes_to_read:
header += os.read(self.request_rfd, bytes_to_read)
bytes_to_read = REQUEST_HEADER.size - len(header)
id, ipa, ipb, ipc, ipd, port, head_len, body_len = REQUEST_HEADER.unpack(header)
head = StringIO()
bytes_to_read = head_len
while bytes_to_read:
head.write(os.read(self.request_rfd, bytes_to_read))
bytes_to_read = head_len - head.tell()
body = StringIO()
bytes_to_read = body_len
while bytes_to_read:
body.write(os.read(self.request_rfd, bytes_to_read))
bytes_to_read = body_len - body.tell()
self.request_sem.release()
return id, (".".join(map(str, [ipa, ipb, ipc, ipd])), port), head.getvalue(), body.getvalue()
def submit_response(self, id, response):
self.response_sem.acquire()
response_output = response.output()
keep_alive = "\x01" if response.headers.get("Connection") == "Keep-Alive" else "\x00"
os.write(self.response_wfd, RESPONSE_HEADER.pack(id, len(response_output)) + response_output + keep_alive)
self.response_sem.release()
def get_response(self):
return self.response_reader.read()
示例11: ForkingWorker
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
class ForkingWorker(BaseWorker):
def __init__(self, num_processes=1):
# Set up sync primitives, to communicate with the spawned children
self._semaphore = Semaphore(num_processes)
self._slots = Array('i', [0] * num_processes)
def spawn_child(self):
"""Forks and executes the job."""
self._semaphore.acquire() # responsible for the blocking
# Select an empty slot from self._slots (the first 0 value is picked)
# The implementation guarantees there will always be at least one empty slot
for slot, value in enumerate(self._slots):
if value == 0:
break
# The usual hardcore forking action
child_pid = os.fork()
if child_pid == 0:
# Within child
# Disable signal handlers
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTERM, signal.SIG_IGN)
random.seed()
try:
self.fake_work()
finally:
# This is the new stuff. Remember, we're in the child process
# currently. When all work is done here, free up the current
# slot (by writing a 0 in the slot position). This
# communicates to the parent that the current child has died
# (so can safely be forgotten about).
self._slots[slot] = 0
self._semaphore.release()
os._exit(0)
else:
# Within parent, keep track of the new child by writing its PID
# into the first free slot index.
self._slots[slot] = child_pid
def wait_for_children(self):
for child_pid in self._slots:
if child_pid != 0:
os.waitpid(child_pid, 0)
def get_id(self):
return os.getpid()
示例12: definyPeerTrading
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def definyPeerTrading(self):
value =0;
peerAux =""
semaphore = Semaphore()
semaphore.acquire()
for peer,trust in self.__peersTrading.iteritems():
if trust >= value:
value = trust
peerAux = peer
semaphore.release()
return (peerAux,value)
示例13: removeNeighbor
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def removeNeighbor(self, source, target):
if (not self.__layout.has_key(source.getId())) and (not self.__layout.has_key(target.getId())) :
return False
if source.hasNeighbor(target):
return False
if target.hasNeighbor(source):
return False
semaphore = Semaphore()
semaphore.acquire()
flag = source.removeNeighbor(target)
target.removeNeighbor(source)
semaphore.release()
return flag
示例14: main
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
def main():
gps_n = Semaphore(0)
gps_s = Semaphore(1)
gps_coords_stack = Manager().list()
gps = GPS(gps_coords_stack, gps_n, gps_s)
gps.start()
# Get the first position
z = gps.getPosition()
dt = 0.05
range_std = 5. # Means meters
# Instantiate the filter
filterk = ExtendedKalmanFilter(2, 1, 0) # 1 type of value of position, but in 2 dimensions. sensor provides position in (x,y) so use 2
# Insert first position
filterk.x = array(z)
# Pretty sure this sets up the taylor series
filterk.F = eye(2) + array([[0,1], [0,0]])*dt
# Sets the uncertainty
filterk.R = np.diag([range_std**2])
# Trains it using white noise?
filterk.Q[0:2, 0:2] = Q_discrete_white_noise(2, dt=dt, var=0.1)
filterk.Q[2, 2] = 0.1
# Covariance matrix
filterk.P *= 50
for i in range(10):
# Pull a value from the GPS stack
gps_n.acquire()
gps_s.acquire()
result = gps_coords_stack.pop()
gps_s.release()
# Put new z value in
filterk.predict_update(array(result), HJacobian_at, hx) #this maaaaay need to be formatted differently, otherwise just put the longitude and lattitude as an array [x,y]
# Get the predicted value
np.append(xs, filterk.x)
print(filterk.x)
示例15: EventMasterProcess
# 需要导入模块: from multiprocessing import Semaphore [as 别名]
# 或者: from multiprocessing.Semaphore import release [as 别名]
class EventMasterProcess(SatoriProcess):
def __init__(self):
super(EventMasterProcess, self).__init__('event master')
self.sem = Semaphore(0)
def do_run(self):
listener = Listener(address=(settings.EVENT_HOST, settings.EVENT_PORT))
master = Master(mapper=TrivialMapper())
master.listen(listener)
self.sem.release()
master.run()
def start(self, *args, **kwargs):
super(EventMasterProcess, self).start(*args, **kwargs)
while True:
if self.sem.acquire(False):
return
if not self.is_alive():
raise RuntimeError('Event master failed to start')
sleep(0)