回答編集履歴
9
非同期開始処理簡略化
test
CHANGED
@@ -84,10 +84,6 @@
|
|
84
84
|
|
85
85
|
|
86
86
|
|
87
|
-
loop = asyncio.get_event_loop()
|
88
|
-
|
89
|
-
|
87
|
+
asyncio.run(execute(functions))
|
90
|
-
|
91
|
-
loop.close()
|
92
88
|
|
93
89
|
```
|
8
関数名変更
test
CHANGED
@@ -58,7 +58,7 @@
|
|
58
58
|
|
59
59
|
|
60
60
|
|
61
|
-
def
|
61
|
+
def add(value):
|
62
62
|
|
63
63
|
async def closure():
|
64
64
|
|
@@ -78,9 +78,9 @@
|
|
78
78
|
|
79
79
|
|
80
80
|
|
81
|
-
|
81
|
+
add(1)
|
82
82
|
|
83
|
-
|
83
|
+
add(2)
|
84
84
|
|
85
85
|
|
86
86
|
|
7
クロージャーのコード例を変更
test
CHANGED
@@ -54,6 +54,10 @@
|
|
54
54
|
|
55
55
|
|
56
56
|
|
57
|
+
functions = []
|
58
|
+
|
59
|
+
|
60
|
+
|
57
61
|
def f(value):
|
58
62
|
|
59
63
|
async def closure():
|
@@ -62,7 +66,7 @@
|
|
62
66
|
|
63
67
|
print(value)
|
64
68
|
|
65
|
-
|
69
|
+
functions.append(closure)
|
66
70
|
|
67
71
|
|
68
72
|
|
@@ -74,11 +78,9 @@
|
|
74
78
|
|
75
79
|
|
76
80
|
|
77
|
-
f
|
81
|
+
f(1)
|
78
82
|
|
79
|
-
functions.append(f(1))
|
80
|
-
|
81
|
-
f
|
83
|
+
f(2)
|
82
84
|
|
83
85
|
|
84
86
|
|
6
クロージャの例を追記
test
CHANGED
@@ -41,3 +41,51 @@
|
|
41
41
|
loop.close()
|
42
42
|
|
43
43
|
```
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
クロージャの例
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```python
|
52
|
+
|
53
|
+
import asyncio
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
def f(value):
|
58
|
+
|
59
|
+
async def closure():
|
60
|
+
|
61
|
+
await asyncio.sleep(value)
|
62
|
+
|
63
|
+
print(value)
|
64
|
+
|
65
|
+
return closure
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
async def execute(functions):
|
70
|
+
|
71
|
+
for func in functions:
|
72
|
+
|
73
|
+
await func()
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
functions = []
|
78
|
+
|
79
|
+
functions.append(f(1))
|
80
|
+
|
81
|
+
functions.append(f(2))
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
loop = asyncio.get_event_loop()
|
86
|
+
|
87
|
+
loop.run_until_complete(execute(functions))
|
88
|
+
|
89
|
+
loop.close()
|
90
|
+
|
91
|
+
```
|
5
C#プログラムに合わせたコードに修正
test
CHANGED
@@ -8,17 +8,13 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
async def f
|
11
|
+
async def f():
|
12
12
|
|
13
13
|
await asyncio.sleep(1)
|
14
14
|
|
15
15
|
print(1)
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
async def f2():
|
20
|
-
|
21
|
-
await asyncio.sleep(
|
17
|
+
await asyncio.sleep(1)
|
22
18
|
|
23
19
|
print(2)
|
24
20
|
|
@@ -32,7 +28,9 @@
|
|
32
28
|
|
33
29
|
|
34
30
|
|
35
|
-
functions =
|
31
|
+
functions = []
|
32
|
+
|
33
|
+
functions.append(f)
|
36
34
|
|
37
35
|
|
38
36
|
|
4
動作確認したソースコードに修正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
これでいかがですか?
|
2
2
|
|
3
3
|
|
4
4
|
|
@@ -6,11 +6,11 @@
|
|
6
6
|
|
7
7
|
import asyncio
|
8
8
|
|
9
|
-
|
9
|
+
|
10
10
|
|
11
11
|
async def f1():
|
12
12
|
|
13
|
-
asyncio.sleep(1)
|
13
|
+
await asyncio.sleep(1)
|
14
14
|
|
15
15
|
print(1)
|
16
16
|
|
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
async def f2():
|
20
20
|
|
21
|
-
asyncio.sleep(2)
|
21
|
+
await asyncio.sleep(2)
|
22
22
|
|
23
23
|
print(2)
|
24
24
|
|
@@ -34,6 +34,12 @@
|
|
34
34
|
|
35
35
|
functions = f1, f2
|
36
36
|
|
37
|
+
|
38
|
+
|
39
|
+
loop = asyncio.get_event_loop()
|
40
|
+
|
37
|
-
execute(functions)
|
41
|
+
loop.run_until_complete(execute(functions))
|
42
|
+
|
43
|
+
loop.close()
|
38
44
|
|
39
45
|
```
|
3
sleepの対応を忘れていたので修正
test
CHANGED
@@ -4,13 +4,13 @@
|
|
4
4
|
|
5
5
|
```python
|
6
6
|
|
7
|
-
import
|
7
|
+
import asyncio
|
8
8
|
|
9
9
|
|
10
10
|
|
11
11
|
async def f1():
|
12
12
|
|
13
|
-
|
13
|
+
asyncio.sleep(1)
|
14
14
|
|
15
15
|
print(1)
|
16
16
|
|
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
async def f2():
|
20
20
|
|
21
|
-
|
21
|
+
asyncio.sleep(2)
|
22
22
|
|
23
23
|
print(2)
|
24
24
|
|
2
C#と同等の処理に変更
test
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
|
1
|
+
動作確認はできてませんが、これでいけませんか?
|
2
|
-
|
3
|
-
async/awaitを使うにしても、以下と同様に書けると思います。
|
4
2
|
|
5
3
|
|
6
4
|
|
@@ -10,7 +8,7 @@
|
|
10
8
|
|
11
9
|
|
12
10
|
|
13
|
-
def f1():
|
11
|
+
async def f1():
|
14
12
|
|
15
13
|
time.sleep(1)
|
16
14
|
|
@@ -18,7 +16,7 @@
|
|
18
16
|
|
19
17
|
|
20
18
|
|
21
|
-
def f2():
|
19
|
+
async def f2():
|
22
20
|
|
23
21
|
time.sleep(2)
|
24
22
|
|
@@ -26,11 +24,11 @@
|
|
26
24
|
|
27
25
|
|
28
26
|
|
29
|
-
def execute(functions):
|
27
|
+
async def execute(functions):
|
30
28
|
|
31
29
|
for func in functions:
|
32
30
|
|
33
|
-
func()
|
31
|
+
await func()
|
34
32
|
|
35
33
|
|
36
34
|
|
1
説明追記
test
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
async/awaitを使わなくてもできますが、async/awaitを使わないといけませんか?
|
2
|
+
|
3
|
+
async/awaitを使うにしても、以下と同様に書けると思います。
|
2
4
|
|
3
5
|
|
4
6
|
|