本文整理汇总了Python中multiprocessing.Lock类的典型用法代码示例。如果您正苦于以下问题:Python Lock类的具体用法?Python Lock怎么用?Python Lock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Lock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
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()
示例2: __init__
def __init__(self, href_tnum):
super(HrefProcess, self).__init__()
self.href_tnum = href_tnum
self.url_set = set() # hash list
self.img_set = set() # hash list
self.url_set_mutex = Lock()
self.img_set_mutex = Lock()
示例3: IPClusterPool
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:
示例4: launch_workers
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()
示例5: SMS_split
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
示例6: WSRunner
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
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: setup
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
示例9: ImageLogger
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()
示例10: say_hello
def say_hello(name='world', **kwargs):
"""@todo: Docstring for say_hello
"""
l = Lock()
l.acquire()
print "Hello, %s" % name, kwargs
l.release()
示例11: __setstate__
def __setstate__(self, d):
from multiprocessing import Lock
self.__dict__ = d
self.gal_lock = Lock()
self.psf_lock = Lock()
self.loaded_lock = Lock()
self.noise_lock = Lock()
pass
示例12: ObjectManager
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
示例13: __init__
def __init__(self, file_name=None, image_dir=None, dir=None, preload=False,
noise_dir=None, logger=None, _nobjects_only=False):
from galsim._pyfits import pyfits
self.file_name, self.image_dir, self.noise_dir = \
_parse_files_dirs(file_name, image_dir, dir, noise_dir)
self.cat = pyfits.getdata(self.file_name)
self.nobjects = len(self.cat) # number of objects in the catalog
if _nobjects_only: return # Exit early if that's all we needed.
ident = self.cat.field('ident') # ID for object in the training sample
# We want to make sure that the ident array contains all strings.
# Strangely, ident.astype(str) produces a string with each element == '1'.
# Hence this way of doing the conversion:
self.ident = [ "%s"%val for val in ident ]
self.gal_file_name = self.cat.field('gal_filename') # file containing the galaxy image
self.psf_file_name = self.cat.field('PSF_filename') # file containing the PSF image
# Add the directories:
self.gal_file_name = [ os.path.join(self.image_dir,f) for f in self.gal_file_name ]
self.psf_file_name = [ os.path.join(self.image_dir,f) for f in self.psf_file_name ]
# We don't require the noise_filename column. If it is not present, we will use
# Uncorrelated noise based on the variance column.
try:
self.noise_file_name = self.cat.field('noise_filename') # file containing the noise cf
self.noise_file_name = [ os.path.join(self.noise_dir,f) for f in self.noise_file_name ]
except:
self.noise_file_name = None
self.gal_hdu = self.cat.field('gal_hdu') # HDU containing the galaxy image
self.psf_hdu = self.cat.field('PSF_hdu') # HDU containing the PSF image
self.pixel_scale = self.cat.field('pixel_scale') # pixel scale for image (could be different
# if we have training data from other datasets... let's be general here and make it a
# vector in case of mixed training set)
self.variance = self.cat.field('noise_variance') # noise variance for image
self.mag = self.cat.field('mag') # apparent magnitude
self.band = self.cat.field('band') # bandpass in which apparent mag is measured, e.g., F814W
self.weight = self.cat.field('weight') # weight factor to account for size-dependent
# probability
self.saved_noise_im = {}
self.loaded_files = {}
self.logger = logger
# The pyfits commands aren't thread safe. So we need to make sure the methods that
# use pyfits are not run concurrently from multiple threads.
from multiprocessing import Lock
self.gal_lock = Lock() # Use this when accessing gal files
self.psf_lock = Lock() # Use this when accessing psf files
self.loaded_lock = Lock() # Use this when opening new files from disk
self.noise_lock = Lock() # Use this for building the noise image(s) (usually just one)
# Preload all files if desired
if preload: self.preload()
self._preload = preload
示例14: run
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)
示例15: __init__
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