本文整理汇总了Python中multiprocessing.Lock.release方法的典型用法代码示例。如果您正苦于以下问题:Python Lock.release方法的具体用法?Python Lock.release怎么用?Python Lock.release使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Lock
的用法示例。
在下文中一共展示了Lock.release方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ImageLogger
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class ImageLogger(object):
def __init__(self, dev_id):
config = ConfigParser.ConfigParser()
config.read(path.dirname(path.realpath(__file__))+"/condor.ini")
storage_root = config.get('condor', 'storage')
self.storage = path.join(storage_root, str(dev_id))
if not path.isdir(self.storage):
mkdir(self.storage)
self.l = Lock()
def image(self, img, name =''):
self.l.acquire()
if not name:
name = str(uuid.uuid4())
imgpath = path.join(self.storage, "%s.png" % (name))
if path.isfile(imgpath):
i = 1
imgpath = path.join(self.storage, "%s-%s.png" % (name, i))
while path.isfile(imgpath):
i += 1
imgpath = path.join(self.storage, "%s-%s.png" % (name, i))
cv2.imwrite(imgpath,img)
self.l.release()
示例2: ClientProcess
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class ClientProcess(Process):
def __init__(self):
Process.__init__(self)
self.parent, self.child = Pipe()
self.state = Lock()
self.start()
def run(self):
while 1:
active_clients = self.child.recv()
self.state.acquire()
client_socket = socket.fromfd(reduction.recv_handle(self.child), socket.AF_INET, socket.SOCK_STREAM)
self.client_process(client_socket, active_clients)
self.state.release()
def client_process(self, sock, active_clients):
while 1:
data = protocol.recv(sock)
if not data:
return sock.close()
encryption = TripleDESCipher(data["key"])
if data["encryption"] == 1:
protocol.send(sock, (active_clients, encryption.encrypt(data["message"])))
else:
protocol.send(sock, (active_clients, encryption.decrypt(data["message"])))
def add_task(self):
if self.state.acquire(False):
self.parent.send(True)
self.state.release()
return True
return False
示例3: __init__
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class Plotter:
def __init__(self):
self.getMap()
self.resolution = 0.05
self.lock = Lock()
#rospy.Subscriber("racecar/mcl/current_particles", PoseArray, self.updatePath) # from MCL
def getMap(self):
image = mapper.read_pgm("mapping/realmap.pgm", byteorder='<')
croppedMap = mapper.hardCrop(image)
self.map = np.flipud(np.rot90(np.array([np.array([item for item in row]) for row in croppedMap])))
def get_path(self):
self.lock.aquire()
self.path = np.load("path.npy")
self.lock.release()
#pyx_file = os.path.join()
def plot_path(self):
print 'plotting path'
print len(self.map)
print len(self.path)
for (x_m, y_m, w) in self.path[:11500]:
x, y = sensor_update.meters_to_pixel([self.map], self.resolution, x_m, y_m)
self.map[min(x+40, 220)][y] = 0
plt.imshow(self.map, plt.cm.gray)
plt.show()
示例4: SMS_split
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class SMS_split(object):
'''split the sms_list for sending '''
def __init__(self,package_size,phone_list):
self.phone_list = phone_list
self.package_size = package_size
self.package = [elem for elem in range(package_size)]
self._lock = Lock()
def __iter__(self):
#the number of sms which already have be splited
self.current_spot = 0
return self
def next(self):
self._lock.acquire()
try:
if (self.current_spot >= len(self.phone_list)):
self.current_spot = len(self.phone_list)
raise StopIteration
self.package = self.phone_list[self.current_spot : \
self.current_spot+self.package_size]
self.current_spot += self.package_size
finally:
self._lock.release()
return self.package
def set_package_size(self, package_size):
self.package_size = package_size
def get_package_size(self):
return self.package_size
def get_already_send_num(self):
return self.current_spot
示例5: launch_workers
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
def launch_workers(outfile, start_index, end_index, score_flag, force, verbose):
BASE_URL = "http://www.ign.com/games/all-ajax?startIndex="
# Synchronization Tools
num_workers = Semaphore(MAX_NUM_PROCESSES)
outfile_lock = Lock()
urlopen_lock = Lock()
stderr_lock = Lock()
print_lock = Lock()
# Write the categories
if (outfile != None):
outfile.write("title,link,platform,publisher,score,date\n")
# Launch the workers
processes = []
curr_index = start_index;
while curr_index <= end_index:
curr_url = BASE_URL + str(curr_index)
worker = Process(target=open_url_and_parse,
args=(outfile, curr_url, score_flag, force, verbose,
outfile_lock, urlopen_lock, stderr_lock, print_lock,
num_workers))
processes.append(worker)
if verbose:
print_lock.acquire()
print "Launching worker for url: %s" % curr_url
print_lock.release()
num_workers.acquire()
worker.start()
curr_index += INDEX_INCREMENT;
for p in processes:
p.join()
示例6: WSRunner
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class WSRunner(Process):
def __init__(self, url, d):
Process.__init__(self)
self.url = url
self.d = d
self.lock = Lock()
self.lock.acquire()
@asyncio.coroutine
def ws(self):
websocket = yield from websockets.connect(self.url)
update_count = 0
self.lock.release()
while update_count < num_loc_updates * num_threads:
update = yield from websocket.recv()
update_count += 1
j = json.loads(update)
lat = float(j['lat'])
lng = float(j['lng'])
player_id = j['player_id']
self.d[player_id] = (lat, lng)
websocket.close()
def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.ws())
示例7: ImageLogger
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class ImageLogger(object):
def __init__(self, root):
self.storage = root
if not path.isdir(self.storage):
mkdir(self.storage)
self.l = Lock()
self.fmt = 'jpg'
def image(self,dev_id, img, ts=None):
self.l.acquire()
if ts:
name = datetime.datetime.fromtimestamp(ts).replace(microsecond=0).strftime('%Y%m%d%H%M%S')
else:
name = str(uuid.uuid4())
image_path = path.join(self.storage,str(dev_id))
if not path.isdir(image_path):
mkdir(image_path)
imgpath = path.join(image_path, "%s.%s" % (name, self.fmt))
i = 0
while path.isfile(imgpath):
imgpath = path.join(image_path, "%s-%s.%s" % (name, i, self.fmt))
i += 1
cv2.imwrite(imgpath, img)
self.l.release()
示例8: say_hello
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
def say_hello(name='world', **kwargs):
"""@todo: Docstring for say_hello
"""
l = Lock()
l.acquire()
print "Hello, %s" % name, kwargs
l.release()
示例9: ReoderBuffer
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class ReoderBuffer(object):
def __init__(self, out_buffer, size=10):
self.out_buffer = out_buffer
self.size = size
self._array = Manager().list(range(self.size))
self._lock = Lock()
self._min = 0
self._max = self.size
self._count = 0
self.update_control()
def update_control(self):
self._control = dict()
for e, i in enumerate(xrange(self._min, self._max)):
self._control.update({i: e})
def put(self, block_num, data):
self._lock.acquire()
if block_num not in self._control.keys():
self._lock.release()
raise IndexError
self._array[self._control[block_num]] = data
self._count += 1
if self._count == self.size: self.sync()
self._lock.release()
def sync(self):
for i in xrange(self._count):
data = self._array[i]
self.out_buffer.put(data)
self._min += self._count
self._max += self._count
self._count = 0
self.update_control()
示例10: run
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
def run(self):
signal.signal(signal.SIGINT, signal.SIG_IGN)
l = Lock()
l.acquire()
pd = pcapy.open_live(self.iface_name, ethernet.ETHERNET_MTU, 0, 100)
pcap_filter = "ether proto %s" % hex(frames.WiwoFrame.ethertype) \
+ " and (ether[14:1] = 0x07 or ether[14:1] = 0x08)"
pd.setfilter(pcap_filter)
l.release()
pd.loop(-1, self.frame_handler)
示例11: __init__
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class __generator:
def __init__(self):
self.lock = Lock()
self.lastId = 0
def getId(self):
self.lock.acquire()
self.lastId += 1
self.lock.release()
return self.lastId
示例12: DataLoader
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class DataLoader(object):
def __init__(self, params, db, nn_db):
self.lock = Lock()
self.db = db
self.cur = db.length
self.im_shape = params['im_shape']
self.nn_shape = params['nn_shape']
self.hist_eq = params['hist_eq']
self.indexes = np.arange(db.length)
self.shuffle = params['shuffle']
self.subtract_mean = params['subtract_mean']
if self.subtract_mean:
self.mean_img = self.db.read_mean_img(self.im_shape)
self.im_shape = params['im_shape']
self.load_nn = params['load_nn']
self.nn_query_size = params['nn_query_size']
if self.load_nn:
self.nn_db = nn_db
#nn_ignore = 1 if db.db_root == nn_db.db_root else 0
nn_ignore = 0
self.nn = NN(nn_db, params['nn_db_size'], nn_ignore)
def load_next_data(self):
nid = self.get_next_id()
jp, imgs, segs = self.db.read_instance(nid, size=self.im_shape)
item = {'jp':jp}
for i in xrange(len(imgs)):
img = imgs[i]
if self.hist_eq:
img = correct_hist(img)
item.update({'img_' + shape_str(self.im_shape[i]):img.transpose((2,0,1)), 'seg_' + shape_str(self.im_shape[i]): segs[i]})
if self.load_nn:
nn_id = self.nn.nn_ids(jp, self.nn_query_size)
if hasattr(nn_id, '__len__'):
nn_id = random.choice(nn_id)
nn_jp, nn_imgs, nn_segs = self.nn_db.read_instance(nn_id, size=self.nn_shape)
item.update({'nn_jp':nn_jp})
for i in xrange(len(nn_imgs)):
nn_img = nn_imgs[i]
if self.hist_eq:
nn_img = correct_hist(nn_img)
item.update({'nn_img_' + shape_str(self.nn_shape[i]):nn_img.transpose((2,0,1)), 'nn_seg_' + shape_str(self.nn_shape[i]): nn_segs[i]})
return item
def get_next_id(self):
self.lock.acquire()
if self.cur >= len(self.indexes) - 1:
self.cur = 0
if self.shuffle:
random.shuffle(self.indexes)
else:
self.cur += 1
self.lock.release()
return self.indexes[self.cur]
示例13: initialize_proposer
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
def initialize_proposer(self):
"""
Intializes a proposer process that acts as a proposer Paxos member
- creates listening socket for client connections
- initializes connections to other server connections
- starts main loop for proposer which reads proposal requests off
a queue of requests
- server_list is a list of pairs (host, port)
"""
# the msg type need to handle dup
proposer_msg_types = [
MESSAGE_TYPE.PREPARE_ACK,
MESSAGE_TYPE.PREPARE_NACK,
MESSAGE_TYPE.ACCEPT_ACK,
]
msg_history = set()
def if_dup(msg, msg_history):
# handle duplication
if msg.msg_type in proposer_msg_types:
msg_signature = (
msg.msg_type,
msg.value,
msg.proposal,
msg.r_proposal,
msg.client_id,
msg.instance,
msg.origin_id)
if msg_signature in msg_history:
# dup, pass
# print "dup msg received by proposer!"
return True
else:
msg_history.add(msg_signature)
return False
# counter for proposer number
proposer_num = self.server_id
# log file
write_lock = Lock()
write_lock.acquire()
logfile = open("server" + str(self.server_id) + ".txt", "w+")
write_lock.release()
def send_to_acceptors(msg, server_connections):
assert isinstance(msg, message.message)
# send the proposal to acceptors
for s_socket in server_connections:
try:
s_socket.sendall(pickle.dumps(msg))
except Exception, e:
server_connections.remove(s_socket)
print "{}: ERROR - {}".format(self.DEBUG_TAG, e)
pass
示例14: MessageThrottle
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class MessageThrottle(object):
"""
Send at most throttle_interval message per second to zoom clients
"""
def __init__(self, configuration, clients):
self._interval = configuration.throttle_interval
self._lock = Lock()
self._clients = clients
self._message = None
self._thread = Thread(target=self._run,
name='message_throttler')
self._running = True
def start(self):
self._thread.start()
def add_message(self, message):
self._lock.acquire()
try:
if self._message is None:
self._message = message
else:
self._message.combine(message)
finally:
self._lock.release()
def _run(self):
while self._running:
self._lock.acquire()
try:
if self._message is not None:
logging.debug('Sending message: {0}'
.format(self._message.to_json()))
for client in self._clients:
try:
client.write_message(self._message.to_json())
except IndexError:
logging.debug('Client closed when trying to send '
'update.')
continue
self._message = None
except AttributeError as e:
logging.exception('Exception in MessageThrottle: {0}'.format(e))
finally:
self._lock.release()
time.sleep(float(self._interval))
def stop(self):
if self._thread.is_alive():
self._running = False
self._thread.join()
示例15: __init__
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import release [as 别名]
class DataWriter:
def __init__ (self, targetDir, fps=10.0):
# Create target directory and pose log
if not os.path.exists(targetDir) :
os.mkdir (targetDir)
self.poseFd = open(targetDir+"/pose.csv", 'w')
self.path = targetDir
self.currentImage = None
self.currentPose = None
self.fps = fps
self.bridge = CvBridge()
self.currentTimestamp = 0.0
# Prepare multiprocessing
self.dataEx = Lock()
self.process = mp.Process(target=self.start)
self.end = False
self.process.start()
def start (self):
rate = rospy.Rate(self.fps)
counter = 0
while (self.end == False):
if self.currentImage is not None and self.currentPose is not None :
self.dataEx.acquire()
cpImg = copy (self.currentImage)
cpPose = copy (self.currentPose)
self.dataEx.release()
curImageName = "{0:06d}.jpg".format (counter)
cv2.imwrite (self.path + "/"+curImageName, self.currentImage)
poseStr = "{:.6f} {} {} {} {} {} {} {}".format(
rospy.Time.now().to_sec(),
cpPose.position.x,
cpPose.position.y,
cpPose.position.z,
cpPose.orientation.x,
cpPose.orientation.y,
cpPose.orientation.z,
cpPose.orientation.w)
self.poseFd.write(poseStr+"\n")
counter += 1
rate.sleep()
def close (self):
self.end = True
self.poseFd.close()