在以下代码中返回异步值之前是否需要loop.close()
?
import asyncio
async def request_url(url):
return url
def fetch_urls(x):
loop = asyncio.get_event_loop()
return loop.run_until_complete(asyncio.gather(*[request_url(url) for url in x]))
那就是fetch_urls
应该是这样吗?:
def fetch_urls(x):
loop = asyncio.get_event_loop()
results = loop.run_until_complete(asyncio.gather(*[request_url(url) for url in x]))
loop.close()
return results
如果需要loop.close()
,那么如何在不提出异常的情况下再次调用fetch_urls
:RuntimeError: Event loop is closed
?
A previous post表示最好关闭循环并启动新循环但是它没有指定如何打开新循环?
答案 0 :(得分:2)
您还可以将事件循环保持活动状态,并使用run_until_complete
多次使用import asyncio
async def request_url(url):
return url
def fetch_urls(loop, urls):
tasks = [request_url(url) for url in urls]
return loop.run_until_complete(asyncio.gather(*tasks, loop=loop))
loop = asyncio.get_event_loop()
try:
print(fetch_urls(loop, ['a1', 'a2', 'a3']))
print(fetch_urls(loop, ['b1', 'b2', 'b3']))
print(fetch_urls(loop, ['c1', 'c2', 'c3']))
finally:
loop.close()
将其关闭到程序的末尾:
<?php
/**
* @package Hello_Dolly
* @version 1.6
*/
/*
Plugin Name: Hello Dolly
Plugin URI: https://wordpress.org/plugins/hello-dolly/
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.6
Author URI: http://ma.tt/
*/
function get_json() {
$slices = json_decode(file_get_contents('http://pf.tradetracker.net/?aid=1&type=json&encoding=utf8&fid=251713&categoryType=2&additionalType=2&limit=1000',true));
create_posttype();
if ($slices) {
foreach ($slices->products as $slice) {
$ID = $slice->ID;
$name = $slice->name;
$currency = $slice->price->currency;
$amount = $slice->price->amount;
$URL = $slice->URL;
$images = $slice->images;
$description = $slice->description;
/* $categories = $slice->categories; */
/* $brand = $slice->properties->brand; */
$producttype = $slice->properties->producttype;
$deliveryCosts = $slice->properties->deliveryCosts;
$SKU = $slice->properties->SKU;
$brand_and_type = $slice->properties->brand_and_type;
$thumbnailURL = $slice->properties->thumbnailURL;
$deliveryTime = $slice->properties->deliveryTime;
$imageURLlarge = $slice->properties->imageURLlarge;
$categoryURL = $slice->properties->categoryURL;
$EAN = $slice->properties->EAN;
$variations = $slice->variations;
/* Test to see each of them is correct.
if ($brand)
echo $brand."<br>";
else
echo "Some error";
*/
$my_post = array(
'post_type' => 'products',
'post_title' => $name,
'post_content' => 'This is my content',
'post_status' => 'publish',
'post_author' => 1,
/* 'post_category' => array(8,39) */
);
// Insert the post into the database and return the new post ID
$post_id = wp_insert_post( $my_post, $wp_error );
}
}
}
// Our custom post type function
function create_posttype() {
register_post_type( 'Products',
// CPT Options
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'products'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
add_shortcode( 'display_data', 'get_json' );
?>
答案 1 :(得分:1)
不,异步函数(在这种情况下为request
)不应该关闭事件循环。命令loop.run_until_complete
close 会在事情用完后立即停止事件循环。
fetch_urls
应该是第二个版本 - 也就是说,它将获得一个事件循环,运行事件循环直到没有任何事情要做,然后关闭它loop.close()
。