本文整理汇总了Python中multiprocessing.Lock.acquire方法的典型用法代码示例。如果您正苦于以下问题:Python Lock.acquire方法的具体用法?Python Lock.acquire怎么用?Python Lock.acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Lock
的用法示例。
在下文中一共展示了Lock.acquire方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [as 别名]
def setup():
"""Performs basic setup for the daemon and ADC"""
global exitLock
logger.debug("Setting up ADC")
try:
ADC.setup()
except RuntimeError as e:
logger.critical(
"Attempting to start the BBB GPIO library failed. This can be "
"due to a number of things, including:"
)
logger.critical(
"- Too new a kernel. Adafruit BBIO runs on 3.8.13. Downgrades "
"to the version this is tested with can be done easily via:")
logger.critical(
" apt-get install linux-{image,headers}-3.8.13-bone79")
logger.critical("- Not running on a BBB")
logger.critical("- Conflicting capes")
logger.critical("Raw exception: %s", str(e))
return
tstat = connect() # TODO: retries
logger.debug("Attaching signal handlers")
signal.signal(signal.SIGINT, handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
logger.debug("Building Lock for singal interrupts")
exitLock = Lock()
exitLock.acquire()
logger.debug("Running main loop")
return tstat
示例2: WSRunner
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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())
示例3: ReoderBuffer
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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()
示例4: IPClusterPool
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [as 别名]
class IPClusterPool(object):
def __init__(self, workers):
self.queue_lock = Lock()
self.queue = []
self.workers = {w: None for w in workers}
self.terminated = False
self.closed = False
threading.Thread(target=self._manager).start()
def terminate(self):
self.terminated = True
def join(self):
while self.terminated != True:
pass
def close(self):
self.closed = True
def apply_async(self, job, *job_args):
assert isinstance(job, str), "Job has to be string"
try:
self.queue_lock.acquire()
self.queue.append(IPClusterTask(job, job_args))
return self.queue[-1]
except Exception, e:
raise e
finally:
示例5: launch_workers
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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: ImageLogger
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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()
示例7: ImageLogger
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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()
示例8: ClientProcess
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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
示例9: say_hello
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [as 别名]
def say_hello(name='world', **kwargs):
"""@todo: Docstring for say_hello
"""
l = Lock()
l.acquire()
print "Hello, %s" % name, kwargs
l.release()
示例10: SMS_split
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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
示例11: ObjectManager
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [as 别名]
class ObjectManager(object):
"""A thread management object for single object threads"""
def __init__(self, input_Q=None, output_Q=None, timeout=10, locking=False, lock=None, **kwargs):
super(ObjectManager, self).__init__(**kwargs)
self.input = Queue() if input_Q is None else input_Q
self.output = Queue() if output_Q is None else output_Q
self._timeout = timeout
self._locking = locking
if lock is None:
self._lock = Lock()
else:
self._lock = lock
self.hdr = {}
self.hdr["pid"] = current_process().pid
self.hdr["STATUS"] = self.NORMAL
def __getattr__(self, attr):
"""Call a method on the underlying threaded object"""
def method(*args, **kwargs):
"""A threaded method"""
if self._locking:
self._lock.acquire()
self.input.put((self.hdr, attr, args, kwargs), timeout=self._timeout)
return method
ERROR = "ERROR"
NORMAL = "NORMAL"
@property
def duplicator(self):
"""Arguments required to duplicate this manager"""
return {
"input_Q": self.input,
"output_Q": self.output,
"timeout": self._timeout,
"locking": self._locking,
"lock": self._lock,
}
def retrieve(self, inputs=False, timeout=None):
"""Retrieve a return value off the top of the output queue"""
timeout = self._timeout if timeout is None else timeout
try:
hdr, func, args, kwargs, rvalue = self.output.get(timeout=timeout)
except QFull, QEmpty:
raise ThreadStateError(code=2 ** 3, msg="Subthread Ended")
if self._locking:
self._lock.release()
if hdr["STATUS"] == self.ERROR:
self.clear()
raise rvalue
if inputs:
return func, args, kwargs, rvalue, hdr
else:
return rvalue
示例12: __init__
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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
示例13: run
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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)
示例14: initialize_proposer
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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
示例15: DataLoader
# 需要导入模块: from multiprocessing import Lock [as 别名]
# 或者: from multiprocessing.Lock import acquire [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]