本文整理汇总了Python中multiprocessing.Lock类的典型用法代码示例。如果您正苦于以下问题:Python Lock类的具体用法?Python Lock怎么用?Python Lock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Lock类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
开发者ID:plancherb1,项目名称:6.141-All-Code,代码行数:29,代码来源:plotter.py
示例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()
开发者ID:WilenceYao,项目名称:mm_crawler,代码行数:7,代码来源:mm_crawler.py
示例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:
开发者ID:gmum,项目名称:mlls2015,代码行数:30,代码来源:parallel_computing.py
示例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()
开发者ID:akshayka,项目名称:video-games,代码行数:34,代码来源:IGN_scraper.py
示例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
开发者ID:simpleton,项目名称:sms_splite,代码行数:33,代码来源:sms_splite.py
示例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())
开发者ID:deedy,项目名称:marauders-map,代码行数:26,代码来源:test_multiprocess.py
示例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()
开发者ID:jwcastillo,项目名称:patchcap,代码行数:25,代码来源:log.py
示例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
开发者ID:spresse1,项目名称:remote_thermostat,代码行数:29,代码来源:thermo_daemon.py
示例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()
开发者ID:MirichST,项目名称:patchcap,代码行数:26,代码来源:log.py
示例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()
开发者ID:xiangxiaobaog3,项目名称:codelab,代码行数:7,代码来源:parallel.py
示例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
开发者ID:PaulPrice,项目名称:GalSim,代码行数:8,代码来源:real.py
示例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
开发者ID:alexrudy,项目名称:pystellar,代码行数:58,代码来源:threading.py
示例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
开发者ID:inonchiu,项目名称:GalSim,代码行数:58,代码来源:real.py
示例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)
开发者ID:azizjonm,项目名称:wiwo,代码行数:10,代码来源:receivers.py
示例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
开发者ID:MorS25,项目名称:CopterMap,代码行数:10,代码来源:idGenerator.py
示例16: DataLoader
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]
开发者ID:saszaz,项目名称:caffe,代码行数:55,代码来源:ekf_datalayer.py
示例17: initialize_proposer
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
开发者ID:mohd1012,项目名称:CSE550,代码行数:55,代码来源:server.py
示例18: KNN
class KNN(object):
def __init__(self):
(self.train, self.valid, self.test), _ = load_data(data_path='data/mfcc_{}.npy', theano_shared=False)
self.train_x, self.train_y = self.train
self.test_x, self.test_y = self.test
self.valid_x, self.valid_y = self.valid
self.train_y = self.train_y.reshape(self.train_y.shape[0])
self.test_y = self.test_y.reshape(self.test_y.shape[0])
self.accurate = Value('i', 0)
self.lck = Lock()
# self.unique_artists()
# self.centers = len(self.artists_map.keys())
self.neigh = KNeighborsClassifier(weights='distance', n_jobs=-1, p=1)
def unique_artists(self):
self.artists_map = {}
for artist in self.data_y:
temp = self.artists_map.get(artist, 0)
self.artists_map[artist] = temp + 1
def fit_data(self):
self.neigh.fit(self.train_x, self.train_y)
def test_accuracy(self, st, en, thread_number, var_x, var_y):
for indx in range(st, en):
data_pt = var_x[indx]
temp = self.neigh.predict(data_pt.reshape(1, -1))[0]
if temp == var_y[indx]:
self.lck.acquire()
# print self.accurate.value,
self.accurate.value += + 1
# print self.accurate.value, indx,
# print 'Thread: {}'.format(thread_number)
self.lck.release()
def testing(self, data_x, data_y):
self.accurate.value = 0
processes = []
ranges = range(0, data_y.shape[0], data_y.shape[0] // 10)
for x in range(len(ranges) - 1):
p = Process(target=self.test_accuracy, args=(ranges[x], ranges[x + 1], x, data_x, data_y))
p.start()
processes.append(p)
_ = map(lambda x: x.join(), processes)
self.accuracy_percentage = self.accurate.value * 100. / data_y.shape[0]
print 'Accuracy: {}'.format(self.accuracy_percentage)
print 'Accurate: {}/{}'.format(self.accurate.value, data_y.shape[0])
开发者ID:shub1905,项目名称:project_apollo,代码行数:53,代码来源:knn.py
示例19: __init__
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()
开发者ID:Aand1,项目名称:Autoware,代码行数:53,代码来源:downsamples.py
示例20: sync_queue
class sync_queue(multiprocessing.queues.Queue):
def __init__(self):
self.q = Queue()
self.lock = Lock()
def add(self, item):
self.lock.acquire()
self.q.put(item)
self.lock.release()
def remove(self):
return self.q.get()
开发者ID:italiangrid,项目名称:WMS-Test-Suite,代码行数:13,代码来源:WMS-stress.py
注:本文中的multiprocessing.Lock类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论