In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to solve the callback function problem in the Java loop". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The problem occurs in the callback function inside the loop, with a very simple example:
For x in xrange (3):
1.
Print "requests begin:%s" x
two。
Def callback (respon):
3.
Print x
4.
Print respon.body
5.
Client.fetch ("http://httpbin.org/get?x=%s" x, callback)
This example ignores the wait implementation waiting for the callback function to complete (failure to implement this will result in exiting without getting a result when running as a single file), and AsyncTestCase in tornado.testing provides the relevant functionality
The httpbin.org/get address returns the requested json object, such as:
{
1.
"args": {
two。
"x": "0"
3.
}
4.
"headers": {
5.
"Accept-Encoding": "gzip"
6.
"Connection": "close"
7.
"Host": "httpbin.org"
8.
"X-Request-Id": "95df3c15-7ed0-4a6d-830d-fb9629e66515"
9.
}
10.
"origin": "192.81.129.91"
11.
"url": "http://httpbin.org/get?x=0""
twelve。
}
But in fact, due to the special feature of the callback function: accessing the current value of a local variable in the closure. Easy to know, in the first request
When the url of http://httpbin.org/get?x=0 returns, the loop is already over, and x is already 2, so in fact, although the json returned by httpbin.org tells us that the x in the get parameter is 0, the x accessed in the closure is already 2.
I thought of two solutions. One is to use the variable space when the callback function is constructed, and this parameter is generated when the function is constructed, such as:
Client = AsyncHTTPClient (self.io_loop)
1.
For x in xrange (3):
two。
Def callback (respon,num=x):
3.
Print x, num
4.
Print respon.body
5.
If num = = 2:
6.
Self.stop ()
7.
Client.fetch ("http://httpbin.org/get?x=%s" x, wrap (x))
One is to wrap another layer of closures (which can also be placed outside the for):
Client = AsyncHTTPClient (self.io_loop)
1.
For x in xrange (3):
two。
Def wrap (number):
3.
Num = number
4.
Def callback (respon):
5.
Print x, num
6.
Print respon.body
7.
If num = = 2:
8.
Self.stop ()
9.
Return callback
10.
Client.fetch ("http://httpbin.org/get?x=%s" x, wrap (x))
11.
# wrap is placed outside the for:
twelve。
Client = AsyncHTTPClient (self.io_loop)
13.
Def wrap (number):
14.
Num = number
15.
Def callback (respon):
16.
Print x, num
17.
Print respon.body
18.
If num = = 2:
19.
Self.stop ()
20.
Return callback
21.
For x in xrange (3):
twenty-two。
Client.fetch ("http://httpbin.org/get?x=%s" x, wrap (x))
After thinking about it, the memory footprint of closures should be inevitable? When each item (x) of the loop body is a large memory object, the memory footprint is equivalent to looping through a list without an iterator, and I don't know if there is a more elegant solution.
This is the end of the content of "how to solve the callback function problem in the Java loop". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.