本文整理汇总了Python中multiprocessing.process.Process.terminate方法的典型用法代码示例。如果您正苦于以下问题:Python Process.terminate方法的具体用法?Python Process.terminate怎么用?Python Process.terminate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.process.Process
的用法示例。
在下文中一共展示了Process.terminate方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_litmus_with_authentication
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import terminate [as 别名]
def test_litmus_with_authentication(self):
"""Run litmus test suite on HTTP with authentification.
This test passes
"""
try:
proc = Process(target=run_wsgidav_server, args=(True, False))
proc.daemon = True
proc.start()
time.sleep(1)
try:
self.assertEqual(subprocess.call(["litmus", "http://127.0.0.1:8080/", "tester", "secret"]),
0,
"litmus suite failed: check the log")
except OSError:
print "*" * 70
print "This test requires the litmus test suite."
print "See http://www.webdav.org/neon/litmus/"
print "*" * 70
raise
finally:
proc.terminate()
proc.join()
示例2: Downloader
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import terminate [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: prepare_proxies
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import terminate [as 别名]
def prepare_proxies(configdata):
if configdata[const.PROXY_CONFIG].get(const.PROXY_CONFIG_SOURCE_TYPE, u'1') != u'2':
return
p = Process(group=None, target=fetch_proxy,)
p.start()
p.join()
print u'%s get %d free proxy' % (datetime.datetime.now(),
len(open(u'proxy.txt', u'r').readlines()))
c = Process(group=None, target=valid_proxy,)
c.start()
valid_time = int(configdata[const.PROXY_CONFIG].get(const.PROXY_VALID_TIME))
print u'%s following %d seconds will valid the proxy' % (datetime.datetime.now(), valid_time)
time.sleep(valid_time)
c.terminate()
print u'%s get %d effective proxy' % (datetime.datetime.now(),
len(open(u'enable_proxies.txt', u'r').readlines()))
示例4: WhenFunctionalTestingGameClient
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import terminate [as 别名]
#.........这里部分代码省略.........
#start a new game with the client and validate a GameState object
#is returned
game = self.client.start_new_game()
self.assertTrue(game, game_state.GameState)
game = self.client.get_game_state(game.game_id)
self.assertTrue(game, game_state.GameState)
#move player 1 from start space to hallway
player = game.current_player
player_1_current_space = game.game_board[player.suspect]
move_space = player_1_current_space.connected_spaces[0]
game = self.client.move_player(
player.username, player.suspect, move_space)
self.assertEqual(
game.turn_status, game_state.AWAITING_ACCUSATION_OR_END_TURN)
game = self.client.end_turn(player.username)
player_1_current_space = game.game_board[move_space]
self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)
#move player 2 from start space to hallway
player = game.current_player
player_2_current_space = game.game_board[player.suspect]
move_space = player_2_current_space.connected_spaces[0]
game = self.client.move_player(
player.username, player.suspect, move_space)
self.assertEqual(
game.turn_status, game_state.AWAITING_ACCUSATION_OR_END_TURN)
game = self.client.end_turn(player.username)
player_2_current_space = game.game_board[move_space]
self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)
#move player 1 from hallway to room
player = game.current_player
move_space = player_1_current_space.connected_spaces[0]
game = self.client.move_player(
player.username, player.suspect, move_space)
self.assertEqual(
game.turn_status, game_state.AWAITING_SUGGESTION)
#make suggestion based on room player is currently in
game = self.client.make_suggestion(
player.username, game_state.MUSTARD,
game_state.REVOLVER,
move_space
)
#if there is a player that can prove the suggestion false
#then test the suggestion response
if game.suggestion_response_player:
with self.assertRaises(errors.GameClientException):
game = self.client.move_player(
player.username, player.suspect, move_space)
self.assertEqual(
game.turn_status, game_state.AWAITING_SUGGESTION_RESPONSE)
response_player = game.suggestion_response_player
suggestion = game.current_suggestion
gamecard_item = list(
{suggestion.weapon, suggestion.room, suggestion.suspect}
&
set(card.item for card in response_player.game_cards))[0]
game = self.client.make_suggestion_response(
response_player.username, gamecard_item)
self.assertEqual(
game.turn_status, game_state.AWAITING_ACCUSATION_OR_END_TURN)
game = self.client.end_turn(player.username)
self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)
last_player = player
player = game.current_player
self.assertNotEqual(player.username, last_player.username)
#test accusation
suspect = [
card.item for card in game.case_file
if card.type == game_state.SUSPECT
][0]
weapon = [
card.item for card in game.case_file
if card.type == game_state.WEAPON
][0]
room = [
card.item for card in game.case_file
if card.type == game_state.ROOM
][0]
game = self.client.make_accusation(
player.username, suspect, weapon, room)
for message in game.player_messages:
print message
self.client.destroy_game(game.game_id)
finally:
self.game_server.terminate()
示例5: AuthorizationCodeTestCase
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import terminate [as 别名]
#.........这里部分代码省略.........
stores = store_factory(client_identifier="abc",
client_secret="xyz",
redirect_uris=[redirect_uri])
provider = Provider(access_token_store=stores["access_token_store"],
auth_code_store=stores["auth_code_store"],
client_store=stores["client_store"],
site_adapter=TestSiteAdapter(),
token_generator=Uuid4())
provider.add_grant(AuthorizationCodeGrant(expires_in=120))
provider.add_grant(RefreshToken(expires_in=60))
app = Wsgi(server=provider)
httpd = make_server('', 15486, app,
handler_class=NoLoggingHandler)
queue.put({"result": 0})
httpd.serve_forever()
except Exception as e:
queue.put({"result": 1, "error_message": str(e)})
def run_client(queue):
try:
app = ClientApplication(
callback_url="http://127.0.0.1:15487/callback",
client_id="abc",
client_secret="xyz",
provider_url="http://127.0.0.1:15486")
httpd = make_server('', 15487, app,
handler_class=NoLoggingHandler)
queue.put({"result": 0})
httpd.serve_forever()
except Exception as e:
queue.put({"result": 1, "error_message": str(e)})
uuid_regex = "^[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}-[a-z0-9]{12}$"
ready_queue = Queue()
self.provider = Process(target=run_provider, args=(ready_queue,))
self.provider.start()
provider_started = ready_queue.get()
if provider_started["result"] != 0:
raise Exception("Error starting Provider process with message"
"'{0}'".format(provider_started["error_message"]))
self.client = Process(target=run_client, args=(ready_queue,))
self.client.start()
client_started = ready_queue.get()
if client_started["result"] != 0:
raise Exception("Error starting Client Application process with "
"message '{0}'"
.format(client_started["error_message"]))
access_token_result = urlopen("http://127.0.0.1:15487/app").read()
access_token_data = json.loads(access_token_result.decode('utf-8'))
self.assertEqual(access_token_data["token_type"], "Bearer")
self.assertEqual(access_token_data["expires_in"], 120)
self.assertRegexpMatches(access_token_data["access_token"],
uuid_regex)
self.assertRegexpMatches(access_token_data["refresh_token"],
uuid_regex)
request_data = {"grant_type": "refresh_token",
"refresh_token": access_token_data["refresh_token"],
"client_id": "abc",
"client_secret": "xyz"}
refresh_token_result = urlopen(
"http://127.0.0.1:15486/token",
urlencode(request_data).encode('utf-8')
)
refresh_token_data = json.loads(refresh_token_result.read().decode('utf-8'))
self.assertEqual(refresh_token_data["token_type"], "Bearer")
self.assertEqual(refresh_token_data["expires_in"], 120)
self.assertRegexpMatches(refresh_token_data["access_token"],
uuid_regex)
def tearDown(self):
if self.client is not None:
self.client.terminate()
self.client.join()
if self.provider is not None:
self.provider.terminate()
self.provider.join()
示例6: AuthorizationCodeTestCase
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import terminate [as 别名]
#.........这里部分代码省略.........
queue.put({"result": 1, "error_message": str(e)})
ready_queue = Queue()
self.server = Process(target=run_provider, args=(ready_queue,))
self.server.start()
provider_started = ready_queue.get()
if provider_started["result"] != 0:
raise Exception("Error starting Provider process with message"
"'{0}'".format(provider_started["error_message"]))
self.client = Process(target=run_client, args=(ready_queue,))
self.client.start()
client_started = ready_queue.get()
if client_started["result"] != 0:
raise Exception("Error starting Client Application process with "
"message '{0}'"
.format(client_started["error_message"]))
self.access_token()
def test_wsgi_404(self):
def run_provider(queue):
try:
provider = create_provider()
app = Application(provider=provider)
httpd = make_server('', 15486, app,
handler_class=NoLoggingHandler)
queue.put({"result": 0})
httpd.serve_forever()
except Exception as e:
queue.put({"result": 1, "error_message": str(e)})
ready_queue = Queue()
self.server = Process(target=run_provider, args=(ready_queue,))
self.server.start()
provider_started = ready_queue.get()
if provider_started["result"] != 0:
raise Exception("Error starting Provider process with message"
"'{0}'".format(provider_started["error_message"]))
try:
urlopen("http://127.0.0.1:15486/invalid-path").read()
except HTTPError as e:
self.assertEqual(404, e.code)
def access_token(self):
uuid_regex = "^[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}-[a-z0-9]{12}$"
try:
access_token_result = urlopen("http://127.0.0.1:15487/app").read()
except HTTPError as e:
print(e.read())
exit(1)
access_token_data = json.loads(access_token_result.decode('utf-8'))
self.assertEqual(access_token_data["token_type"], "Bearer")
self.assertEqual(access_token_data["expires_in"], 120)
self.assertRegexpMatches(access_token_data["access_token"],
uuid_regex)
self.assertRegexpMatches(access_token_data["refresh_token"],
uuid_regex)
request_data = {"grant_type": "refresh_token",
"refresh_token": access_token_data["refresh_token"],
"client_id": "abc",
"client_secret": "xyz"}
refresh_token_result = urlopen(
"http://127.0.0.1:15486/token",
urlencode(request_data).encode('utf-8')
)
refresh_token_data = json.loads(refresh_token_result.read().decode('utf-8'))
self.assertEqual(refresh_token_data["token_type"], "Bearer")
self.assertEqual(refresh_token_data["expires_in"], 120)
self.assertRegexpMatches(refresh_token_data["access_token"],
uuid_regex)
def tearDown(self):
if self.client is not None:
self.client.terminate()
self.client.join()
if self.server is not None:
self.server.terminate()
self.server.join()
示例7: RemoteTest
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import terminate [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)
#.........这里部分代码省略.........
示例8: MongoRedis
# 需要导入模块: from multiprocessing.process import Process [as 别名]
# 或者: from multiprocessing.process.Process import terminate [as 别名]
class MongoRedis(object):
def __init__(self, mongo_db, collection_name='cache'):
# Ensure index
if not isinstance(mongo_db, Database):
raise ValueError(
'mongo_db must be instance of pymongo.database.Database')
self.col = mongo_db[collection_name]
self.col.ensure_index('k', unique=True)
self.col.ensure_index('exp')
self.prune_expired()
def start(self):
"""
Starts the background process that prunes expired items
"""
def task():
while True:
self.prune_expired()
pytime.sleep(.5)
self.processs = Process(target=task)
self.processs.start()
def end(self):
"""
End the background process that prunes expired items
"""
if not hasattr(self, 'process'):
self.processs.terminate()
def prune_expired(self):
"""
Deletes expired keys from the db, returns count deleted
"""
now = pytime.time()
result = self.col.remove({'exp': {'$exists': True, '$lte': now}})
return result['n']
### REDIS COMMANDS ###
def delete(self, *names):
"""
Delete one or more keys specified by ``names``
"""
return self.col.remove({'k': {'$in': names}})['n']
__delitem__ = delete
def expire(self, name, time):
"""
Set an expire flag on key ``name`` for ``time`` seconds. ``time``
can be represented by an integer or a Python timedelta object.
"""
expire_at = pytime.time()
if isinstance(time, datetime.timedelta):
time = time.seconds + time.days * 24 * 3600
expire_at += time
return bool(
self.col.update({'k': name}, {'$set': {'exp': expire_at}})['n'])
def flushdb(self):
"""
Delete all keys in the current database
"""
self.col.remove()
return True
def get(self, name):
"""
Return the value at key ``name``, or None if the key doesn't exist
"""
now = pytime.time()
result = self.col.find_one({'k': name}) or {}
if result.get('exp', now) < now:
return None
return result.get('v')
def set(self, name, value, ex=None, px=None, nx=False, xx=False):
"""
Set the value at key ``name`` to ``value``
``ex`` sets an expire flag on key ``name`` for ``ex`` seconds.
``px`` sets an expire flag on key ``name`` for ``px`` milliseconds.
``nx`` if set to True, set the value at key ``name`` to ``value`` if it
does not already exist.
``xx`` if set to True, set the value at key ``name`` to ``value`` if it
already exists.
"""
upsert = True
expire_at = pytime.time()
if px:
# if isinstance(px, datetime.timedelta):
# ms = int(px.microseconds / 1000)
# px = (px.seconds + px.days * 24 * 3600) * 1000 + ms
# expire_at += px * 0.001
raise NotImplementedError # Millis to fine grained
elif ex:
#.........这里部分代码省略.........