本文整理汇总了Python中multiprocessing.Value类的典型用法代码示例。如果您正苦于以下问题:Python Value类的具体用法?Python Value怎么用?Python Value使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Value类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_heartbeat
def setup_heartbeat(self, client_controller):
cond = multiprocessing.Condition()
s_init_finish = Value('i', 0)
do_sample = Value('i', 0)
do_sample_lock = Lock()
server_process = multiprocessing.Process(
target=self.server_heart_beat,
args=(cond, s_init_finish, do_sample, do_sample_lock))
server_process.daemon = False
server_process.start()
logger.info("Waiting for server init ...")
cond.acquire()
while (s_init_finish.value == 0):
cond.wait()
if s_init_finish.value == 5:
logger.error("Waiting for server init ... FAIL")
raise RuntimeError("server init failed.")
cond.release()
logger.info("Waiting for server init ... Done")
# let all clients start running the benchmark
client_controller.client_run(do_sample, do_sample_lock)
cond.acquire()
s_init_finish.value = 0
cond.release()
return server_process
示例2: run_with_exception_except_test
def run_with_exception_except_test(self):
""" Subclass StoppableExceptionThread and raise exception in method `run_with_exception` """
class IncrementThread(StoppableExceptionThread):
""" Used to test _stop in `run` """
def __init__(self, *args, **kwargs):
self.x = args[0]
StoppableExceptionThread.__init__(self, *args[1:], **kwargs)
def run_with_exception(self):
while not self._stop.is_set():
with self.x.get_lock():
self.x.value += 1
if self.x.value > 5:
raise ValueError('x > 5')
x = Value('i', 0)
st = IncrementThread(x)
st.start()
sleep(1)
assert_equals(st.stopped, False)
with self.assertRaises(ValueError):
st.join()
assert_equals(st.is_alive(), False)
with x.get_lock():
assert_equals(x.value, 6)
示例3: Counter
class Counter(object):
"""
A process-safe counter providing atomic incrementAndGet() and value() functions
"""
def __init__(self, initval=0):
"""
Initialize this counter
Args:
initval (int): set the initialize value of the counter
"""
self.val = Value('i', initval)
def incrementAndGet(self):
"""
Atomically increment this counter, and return the new value stored.
Returns:
int: The updated value of this counter.
"""
with self.val.get_lock():
self.val.value += 1
return self.val.value
def value(self):
"""
Atomically get the current value of this counter.
Returns:
int: The current value of this counter.
"""
with self.val.get_lock():
return self.val.value
示例4: run_stop_test
def run_stop_test(self):
""" Subclass StoppableThread and stop method `run` """
class IncrementThread(StoppableThread):
""" Used to test _stop in `run` """
def __init__(self, *args, **kwargs):
self.x = args[0]
super(IncrementThread, self).__init__(*args[1:], **kwargs)
def run(self):
while not self._stop.is_set():
with self.x.get_lock():
self.x.value += 1
x = Value('i', 0)
st = IncrementThread(x)
st.start()
assert_equals(st.stopped, False)
assert_equals(st.is_alive(), True)
sleep(0.5)
st.stop()
assert_equals(st.stopped, True)
st.join()
assert_equals(st.is_alive(), False)
with x.get_lock():
assert_greater(x.value, 0)
示例5: main
def main():
settings.setup()
try:
import miniupnpc
except:
safeprint("Dependency miniupnpc is not installed. Running in outbound only mode")
settings.config['outbound'] = True
safeprint("settings are:")
safeprint(settings.config)
queue = Queue()
live = Value('b',True)
ear = listener(settings.config['port'],settings.config['outbound'],queue,live,settings.config['server'])
ear.daemon = True
ear.start()
feedback = []
stamp = time()
while queue.empty():
if time() - 5 > stamp:
break #pragma: no cover
try:
feedback = queue.get(False)
except: #pragma: no cover
safeprint("No feedback received from listener")
ext_ip = "" #Does this affect peers?
ext_port = -1 #Does this affect peers?
if feedback != []:
settings.outbound = feedback[0]
if settings.outbound is not True:
ext_ip = feedback[1]
ext_port = feedback[2]
initializePeerConnections(settings.config['port'], ext_ip, ext_port)
live.value = False
示例6: execute_task
def execute_task(self, website: Website, busy: Value, post_id: str, comment_id: str):
busy.value = 1
if os.path.exists("data.json"):
os.remove("data.json")
print("Started crawling task")
process = CrawlerProcess(get_project_settings())
process.crawl("od_links", base_url=website.url)
process.start()
print("Done crawling")
self.db.import_json("data.json", website)
os.remove("data.json")
print("Imported in SQLite3")
if post_id:
# Reply to post
stats = self.db.get_website_stats(website.id)
comment = self.reddit_bot.get_comment({"": stats}, website.id)
print(comment)
if "total_size" in stats and stats["total_size"] > 10000000:
post = self.reddit_bot.reddit.submission(post_id)
self.reddit_bot.reply(post, comment)
pass
else:
self.reddit_bot.log_crawl(post_id)
elif comment_id:
# Reply to comment
stats = self.db.get_website_stats(website.id)
comment = self.reddit_bot.get_comment({"There you go!": stats}, website.id)
print(comment)
reddit_comment = self.reddit_bot.reddit.comment(comment_id)
self.reddit_bot.reply(reddit_comment, comment)
busy.value = 0
print("Done crawling task")
示例7: main
def main():
#Begin Init
settings.setup()
from common.safeprint import safeprint
try:
import miniupnpc
except:
safeprint("Dependency miniupnpc is not installed. Running in outbound only mode")
settings.config['outbound'] = True
safeprint("settings are:")
safeprint(settings.config)
queue = Queue()
live = Value('b',True)
ear = listener(settings.config['port'],settings.config['outbound'],queue,live,settings.config['server'])
ear.daemon = True
ear.items = sync()
ear.start()
mouth = propagator(settings.config['port'] + 1, live)
mouth.daemon = True
mouth.items = ear.items
mouth.start()
feedback = []
stamp = time()
while queue.empty():
if time() - 5 > stamp:
break #pragma: no cover
try:
feedback = queue.get(False)
except: #pragma: no cover
safeprint("No feedback received from listener")
ext_ip = "" #Does this affect peers?
ext_port = -1 #Does this affect peers?
if feedback != []:
settings.outbound = feedback[0]
if settings.outbound is not True:
ext_ip = feedback[1]
ext_port = feedback[2]
initializePeerConnections(settings.config['port'], ext_ip, ext_port)
#End Init
#Begin main loop
if settings.config.get('seed'):
safeprint("Seed mode activated")
try:
while True and not settings.config.get('test'):
sleep(0.1)
except KeyboardInterrupt:
safeprint("Keyboard Interrupt")
elif settings.config.get('server'):
safeprint("Server mode activated")
else:
safeprint("Client mode activated")
#End main loop
#Begin shutdown
safeprint("Beginning exit process")
live.value = False
settings.saveSettings()
saveToFile()
bounty.saveToFile()
示例8: call
def call(args, stdout=None, stderr=None, stdin=None, daemonize=False,
preexec_fn=None, shell=False, cwd=None, env=None):
"""
Run an external command in a separate process and detach it from the current process. Excepting
`stdout`, `stderr`, and `stdin` all file descriptors are closed after forking. If `daemonize`
is True then the parent process exits. All stdio is redirected to `os.devnull` unless
specified. The `preexec_fn`, `shell`, `cwd`, and `env` parameters are the same as their `Popen`
counterparts. Return the PID of the child process if not daemonized.
"""
stream = lambda s, m: s is None and os.open(os.devnull, m) or s
stdout = stream(stdout, os.O_WRONLY)
stderr = stream(stderr, os.O_WRONLY)
stdin = stream(stdin, os.O_RDONLY)
shared_pid = Value('i', 0)
pid = os.fork()
if pid > 0:
os.waitpid(pid, 0)
child_pid = shared_pid.value
del shared_pid
if daemonize:
sys.exit(0)
return child_pid
else:
os.setsid()
proc = subprocess.Popen(args, stdout=stdout, stderr=stderr, stdin=stdin, close_fds=True,
preexec_fn=preexec_fn, shell=shell, cwd=cwd, env=env)
shared_pid.value = proc.pid
os._exit(0)
示例9: _init_visualization_and_io
def _init_visualization_and_io(self, sim):
if self.config.output:
output_cls = io.format_name_to_cls[self.config.output_format]
else:
output_cls = io.LBOutput
if self.config.mode != 'visualization':
return lambda subdomain: output_cls(self.config, subdomain.id)
# basic_fields = sim.fields()
# XXX compute total storage requirements
for subdomain in self.subdomains:
size = reduce(operator.mul, subdomain.size)
vis_lock = mp.Lock()
vis_buffer = Array(ctypes.c_float, size, lock=vis_lock)
vis_geo_buffer = Array(ctypes.c_uint8, size, lock=vis_lock)
subdomain.set_vis_buffers(vis_buffer, vis_geo_buffer)
vis_lock = mp.Lock()
vis_config = Value(io.VisConfig, lock=vis_lock)
vis_config.iteration = -1
vis_config.field_name = ''
vis_config.all_blocks = False
# Start the visualizatione engine.
vis_class = None
for engine in util.get_visualization_engines():
if engine.name == self.config.vis_engine:
vis_class = engine
break
if vis_class is None:
self.config.logger.warning('Requested visualization engine not '
'available.')
try:
vis_class = util.get_visualization_engines().next()
except StopIteration:
self.config.logger.warning(
'No visualization backends available. Falling back to '
'batch mode.')
self.config.mode = 'batch'
return lambda subdomain: output_cls(self.config, subdomain.id)
# Event to signal that the visualization process should be terminated.
self._vis_quit_event = Event()
self._vis_process = Process(
target=lambda: vis_class(
self.config, self.subdomains, self._vis_quit_event,
self._quit_event, vis_config).run(),
name='VisEngine')
self._vis_process.start()
return lambda subdomain: io.VisualizationWrapper(
self.config, subdomain, vis_config, output_cls)
示例10: main
def main():
running = Value(c_int, 1)
readQueue = Queue()
reader = Process(target = Reader("/dev/ttyUSB0", 9600), args = (running, readQueue))
worker = Process(target = Worker(), args = (running, readQueue))
reader.start()
worker.start()
time.sleep(5)
running.value = 0
reader.join()
worker.join()
示例11: __init__
def __init__(self, nomenclature="", width=0., height=0.):
Device.__init__(self, nomenclature, width, height)
self.xpos_left = -0.5 * width
self.xpos_right = 0.5 * width
self.ypos_bottom = -0.5 * height
self.ypos_top = 0.5 * height
self.count_left = Value('i', 0)
self.count_right = Value('i', 0)
self.count_bottom = Value('i', 0)
self.count_top = Value('i', 0)
示例12: _init_visualization_and_io
def _init_visualization_and_io(self, sim):
if self.config.output:
output_cls = io.format_name_to_cls[self.config.output_format]
else:
# Dummy output class. Does not actually save data, but does provide
# utility functions common to all output classes.
output_cls = io.LBOutput
if self.config.mode != 'visualization':
return lambda subdomain: output_cls(self.config, subdomain.id)
# XXX compute total storage requirements
self._vis_geo_queues = []
for subdomain in self.subdomain_specs:
self._vis_geo_queues.append(subdomain.init_visualization())
vis_lock = mp.Lock()
vis_config = Value(io.VisConfig, lock=vis_lock)
vis_config.iteration = -1
vis_config.field_name = ''
vis_config.all_subdomains = False
# Start the visualizatione engine.
vis_class = None
for engine in util.get_visualization_engines():
if engine.name == self.config.vis_engine:
vis_class = engine
break
if vis_class is None:
self.config.logger.warning('Requested visualization engine not '
'available.')
try:
vis_class = util.get_visualization_engines().next()
except StopIteration:
self.config.logger.warning(
'No visualization backends available. Falling back to '
'batch mode.')
self.config.mode = 'batch'
return lambda subdomain: output_cls(self.config, subdomain.id)
# Event to signal that the visualization process should be terminated.
self._vis_quit_event = Event()
self._vis_process = Process(
target=lambda: vis_class(
self.config, self.subdomain_specs, self._vis_quit_event,
self._quit_event, vis_config, self._vis_geo_queues).run(),
name='VisEngine')
self._vis_process.start()
return lambda subdomain: io.VisualizationWrapper(
self.config, subdomain, vis_config, output_cls)
示例13: Control
class Control(object):
"""Shared (long) value for passing control information between main and
worker threads.
Args:
initial_value: Initial value of the shared control variable.
"""
def __init__(self, initial_value=CONTROL_ACTIVE):
self.control = Value('l', initial_value)
def check_value(self, value, lock=False):
"""Check that the current control value == `value`.
Args:
value: The value to check.
lock: Whether to lock the shared variable before checking.
Returns:
True if the values are equal.
"""
return self.get_value(lock=lock) == value
def check_value_positive(self, lock=False):
"""Check that the current control value is positive.
Args:
lock: Whether to lock the shared variable before checking.
"""
return self.get_value(lock=lock) > 0
def get_value(self, lock=True):
"""Returns the current control value.
Args:
lock: Whether to lock the shared variable before checking.
"""
if lock:
with self.control.get_lock():
return self.control.value
else:
return self.control.value
def set_value(self, value):
"""Set the control value. The shared variable is always locked.
Args:
value: The value to set.
"""
with self.control.get_lock():
self.control.value = value
示例14: __init__
class Counter:
def __init__(self):
self.value = Value(ctypes.c_int)
def __enter__(self):
with self.value.get_lock():
self.value.value += 1
def __exit__(self, exc_type, exc_val, exc_tb):
with self.value.get_lock():
self.value.value -= 1
def __repr__(self):
return str(self.value.value)
示例15: camstream
def camstream():
print "CAMera STREAMer (OpenCV " + cv2.__version__ + ")"
print "main(): OS: {}".format(os.name)
# * Start CameraStreamer process
print "main(): Starting CameraStreamer process..."
if os.name == 'nt': # [Windows]
# ** Create shared objects (NOTE only necessary on Windows since it uses a different multiprocessing implementation)
print "main(): [Windows] Creating shared objects..."
# *** Stay alive flag
stayAliveObj = Value(c_bool, True)
# *** Frame counter
frameCountObj = Value('i', 0)
# *** Image array
image = np.zeros((camera_frame_height, camera_frame_width, camera_frame_depth), dtype=np.uint8)
imageShape = image.shape
imageSize = image.size
image.shape = imageSize # flatten numpy array
imageObj = Array(c_ubyte, image) # create a synchronized shared array
# *** Image shape
imageShapeObj = Array('i', imageShape)
cameraStreamerProcess = CameraStreamer(stayAliveObj, frameCountObj, imageObj, imageShapeObj)
else: # [POSIX]
cameraStreamerProcess = CameraStreamer()
# ** Grab generated shared objects to share with other child processes
print "main(): [POSIX] Getting shared objects from CameraStreamer..."
stayAliveObj = cameraStreamerProcess.stayAliveObj
frameCountObj = cameraStreamerProcess.frameCountObj
imageObj = cameraStreamerProcess.imageObj
imageShapeObj = cameraStreamerProcess.imageShapeObj
cameraStreamerProcess.start()
# * Start StreamViewer process
print "main(): Starting StreamViewer process..."
streamViewerProcess = StreamViewer(stayAliveObj, frameCountObj, imageObj, imageShapeObj)
streamViewerProcess.start()
# * Wait for child processes to finish
print "main(): Waiting for child processes to finish..."
try:
streamViewerProcess.join()
cameraStreamerProcess.join()
except KeyboardInterrupt:
stayAliveObj.value = False
streamViewerProcess.join()
cameraStreamerProcess.join()
print "main(): Done."