python asyncio future_Python asyncio.ensure_future方法代碼示例

本文整理匯總了Python中asyncio.ensure_future方法的典型用法代碼示例。如果您正苦於以下問題:Python asyncio.ensure_future方法的具體用法?Python asyncio.ensure_future怎麽用?Python asyncio.ensure_future使用的例子?那麽恭喜您, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在模塊asyncio的用法示例。

在下文中一共展示了asyncio.ensure_future方法的30個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於我們的係統推薦出更棒的Python代碼示例。

示例1: test_websocket_non_regression_bug_105

​點讚 6

# 需要導入模塊: import asyncio [as 別名]

# 或者: from asyncio import ensure_future [as 別名]

def test_websocket_non_regression_bug_105(event_loop, server):

# This test will check a fix to a race condition which happens if the user is trying

# to connect using the same client twice at the same time

# See bug #105

url = f"ws://{server.hostname}:{server.port}/graphql"

print(f"url = {url}")

sample_transport = WebsocketsTransport(url=url)

client = Client(transport=sample_transport)

# Create a coroutine which start the connection with the transport but does nothing

async def client_connect(client):

async with client:

await asyncio.sleep(2 * MS)

# Create two tasks which will try to connect using the same client (not allowed)

connect_task1 = asyncio.ensure_future(client_connect(client))

connect_task2 = asyncio.ensure_future(client_connect(client))

with pytest.raises(TransportAlreadyConnected):

await asyncio.gather(connect_task1, connect_task2)

開發者ID:graphql-python,項目名稱:gql,代碼行數:26,

示例2: listen_message_stream

​點讚 6

# 需要導入模塊: import asyncio [as 別名]

# 或者: from asyncio import ensure_future [as 別名]

def listen_message_stream(self, id_blacklist=None):

id_blacklist = set(id_blacklist or [self.me, ])

loop = asyncio.new_event_loop()

asyncio.set_event_loop(loop)

with aiohttp.ClientSession(loop=loop) as session:

self.aioclient_session = session

tasks = [

asyncio.ensure_future(self.fetch(session, room, id_blacklist))

for room in self.rooms

]

done, _ = loop.run_until_complete(

asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)

)

for d in done:

if d.exception():

raise d.exception()

開發者ID:tuna,項目名稱:fishroom,代碼行數:20,

示例3: async_run

​點讚 6

# 需要導入模塊: import asyncio [as 別名]

# 或者: from asyncio import ensure_future [as 別名]

def async_run(tasks):

"""

run a group of tasks async

Requires the tasks arg to be a list of functools.partial()

"""

if not tasks:

return

# start a new async event loop

loop = asyncio.get_event_loop()

# https://github.com/python/asyncio/issues/258

executor = concurrent.futures.ThreadPoolExecutor(5)

loop.set_default_executor(executor)

async_tasks = [asyncio.ensure_future(async_task(task, loop)) for task in tasks]

# run tasks in parallel

loop.run_until_complete(asyncio.wait(async_tasks))

# deal with errors (exceptions, etc)

for task in async_tasks:

error = task.exception()

if error is not None:

raise error

executor.shutdown(wait=True)

開發者ID:deis,項目名稱:controller,代碼行數:26,

示例4: main

​點讚 6

# 需要導入模塊: import asyncio [as 別名]

# 或者: from asyncio import ensure_future [as 別名]

def main(_):

urls = get_urls_for_shard_group(

FLAGS.urls_dir, FLAGS.shard_id, FLAGS.group_id)

tf.logging.info("Fetching %d URLs for shard %d, group %d",

len(urls), FLAGS.shard_id, FLAGS.group_id)

tf.gfile.MakeDirs(FLAGS.out_dir)

out_fname = tfrecord_fname(FLAGS.out_dir, FLAGS.shard_id)

with utils.timing("group_fetch"):

logging_fnames = {}

if FLAGS.log_samples:

logging_fnames["samples"] = os.path.join(

FLAGS.out_dir, "samples.%d.txt" % FLAGS.shard_id)

loop = asyncio.get_event_loop()

num_written = loop.run_until_complete(asyncio.ensure_future(

fetch_urls(urls,

out_fname,

logging_fnames)))

tf.logging.info("Total URLs: %d", len(urls))

tf.logging.info("Num written: %d", num_written)

tf.logging.info("Coverage: %.1f", (num_written / len(urls)) * 100)

開發者ID:akzaidi,項目名稱:fine-lm,代碼行數:25,

示例5: test_task_local

​點讚 6

# 需要導入模塊: import asyncio [as 別名]

# 或者: from asyncio import ensure_future [as 別名]

def test_task_local() -> None:

local_ = TaskLocal()

queue: asyncio.Queue = asyncio.Queue()

tasks = 2

for _ in range(tasks):

queue.put_nowait(None)

async def _test_local(value: int) -> int:

local_.test = value

await queue.get()

queue.task_done()

await queue.join()

return local_.test

futures = [asyncio.ensure_future(_test_local(value)) for value in range(tasks)]

asyncio.gather(*futures)

for value, future in enumerate(futures):

assert (await future) == value

開發者ID:pgjones,項目名稱:quart,代碼行數:20,

示例6: test_copy_current_app_context

​點讚 6

# 需要導入模塊: import asyncio [as 別名]

# 或者: from asyncio import ensure_future [as 別名]

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值