本文整理汇总了Python中multiprocessing.process.Process.is_alive方法的典型用法代码示例。如果您正苦于以下问题:Python Process.is_alive方法的具体用法?Python Process.is_alive怎么用?Python Process.is_alive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.process.Process
的用法示例。
在下文中一共展示了Process.is_alive方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_library_in_another_process
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import is_alive [as 别名]
def import_library_in_another_process(path, args):
q = Queue(maxsize=1)
p = Process(target=library_initializer, args=(q, path, args))
p.start()
while True:
try:
result = q.get(timeout=0.1)
if isinstance(result, Exception):
raise ImportError(result)
return result
except Empty:
if not p.is_alive():
raise ImportError()
示例2: Downloader
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import is_alive [as 别名]
class Downloader(object):
def __init__(self, timeout=30, retries=100, wait=1):
self.timeout = timeout
self.retries = retries
self.wait = wait
self.manager = SyncManager()
self.manager.start()
def retry_fetch_data(self, url):
market_data = self.fetch_data(url)
retries = 1
while not market_data and retries < self.retries:
print "Retry #%s..." % str(retries)
market_data = self.fetch_data(url)
if market_data:
print "Fetched: " + str(len(market_data))
else:
print "Fetched nothing!"
retries += 1
return market_data
def fetch_data(self, url):
limit = 60
msg = "Downloading " + url[0: min(limit, len(url))]
if len(url) > limit:
msg += "(+" + str(len(url) - limit) + ")"
print msg
return_dict = self.manager.dict()
self.job = Process(target=get_page_data, args=(url, return_dict))
self.job.start()
self.job.join(self.timeout)
if self.job.is_alive():
self.job.terminate()
self.job = None
market_data = None
if 'page' in return_dict:
market_data = return_dict['page']
if self.wait > 0:
time.sleep(self.wait)
return market_data
示例3: RemoteTest
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import is_alive [as 别名]
class RemoteTest(unittest.TestCase):
"""
Test the Component, DataFlow, and VAs when shared remotely.
The test cases are run as "clients" and at start a server is started.
"""
container_name = "test"
def setUp(self):
# Use Thread for debug:
if USE_THREADS:
self.server = Thread(target=ServerLoop, args=(self.container_name,))
else:
self.server = Process(target=ServerLoop, args=(self.container_name,))
self.server.start()
self.count = 0
self.data_arrays_sent = 0
time.sleep(0.1) # give it some time to start
self.rdaemon = Pyro4.Proxy("PYRO:[email protected]/u:"+self.container_name)
self.comp = self.rdaemon.getObject("mycomp")
def tearDown(self):
self.comp.stopServer()
time.sleep(0.1) # give it some time to terminate
if self.server.is_alive():
if not USE_THREADS:
print "Warning: killing server still alive"
self.server.terminate()
# @unittest.skip("simple")
def test_simple(self):
"""
start a component, ping, and stop it
"""
ret = self.comp.ping()
self.assertEqual(ret, "pong", "Ping failed")
# @unittest.skip("simple")
def test_exception(self):
# test it raises
self.assertRaises(MyError, self.comp.bad_call)
# test it raises when wrong argument
self.assertRaises(TypeError, self.comp.ping, ("non needed arg",))
# non existing method
self.assertRaises(AttributeError, self.comp.non_existing_method)
# @unittest.skip("simple")
def test_roattributes(self):
"""
check roattributes
"""
val = self.comp.my_value
self.assertEqual(val, "ro", "Reading attribute failed")
# @unittest.skip("simple")
def test_async(self):
"""
test futures
MyComponent queues the future in order of request
"""
self.comp.set_number_futures(0)
ft1 = self.comp.do_long(2) # long enough we can cancel ft2
ft2 = self.comp.do_long(1) # shorter than ft1
self.assertFalse(ft1.done(), "Future finished too early")
self.assertFalse(ft2.done(), "Future finished too early")
self.assertFalse(ft2.cancelled(), "future doesn't claim being cancelled")
self.assertFalse(ft2.cancelled(), "future doesn't claim being cancelled")
self.assertGreater(ft2.result(), 1) # wait for ft2
self.assertFalse(ft2.cancel(), "could cancel the finished future")
self.assertTrue(ft1.done(), "Future not finished")
self.assertGreater(ft1.result(), 2)
self.assertEqual(self.comp.get_number_futures(), 2)
# @unittest.skip("simple")
def test_unref_futures(self):
"""
test many futures which don't even get referenced
It should behave as if the function does not return anything
"""
self.comp.set_number_futures(0)
expected = 100 # there was a bug with expected > threadpool size (=24)
start = time.time()
for i in range(expected):
self.comp.do_long(0.1)
ft_last = self.comp.do_long(0.1)
ft_last.result()
duration = time.time() - start
self.assertGreaterEqual(duration, expected * 0.1)
#.........这里部分代码省略.........