@@ -14,35 +14,29 @@ def get_random_record(container):
14
14
15
15
@asyncio .coroutine
16
16
def get_random_records (container , limit ):
17
- tasks = [ ]
17
+ pg = yield from container . engines [ 'pg' ]
18
18
results = []
19
- for i in range (limit ):
20
- tasks .append (container .loop .create_task (get_random_record (container )))
21
- yield from asyncio .wait (tasks )
22
- for task in tasks :
23
- results .append (task .result ())
19
+ with (yield from pg .cursor ()) as cur :
20
+ for i in range (limit ):
21
+ yield from cur .execute ('SELECT id AS "Id", randomnumber AS "RandomNumber" FROM world WHERE id=%(idx)s LIMIT 1' ,
22
+ {'idx' : randint (1 , 10000 )})
23
+ results .append ((yield from cur .fetchone ()))
24
+
24
25
return results
25
26
26
27
@asyncio .coroutine
27
- def update_random_record (container ):
28
+ def update_random_records (container , limit ):
29
+ results = []
28
30
pg = yield from container .engines ['pg' ]
29
31
30
- world = yield from get_random_record (container )
31
-
32
32
with (yield from pg .cursor ()) as cur :
33
- yield from cur .execute ('UPDATE world SET randomnumber=%(random_number)s WHERE id=%(idx)s' ,
34
- {'random_number' : randint (1 , 10000 ), 'idx' : world ['Id' ]})
35
- return world
36
-
37
- @asyncio .coroutine
38
- def update_random_records (container , limit ):
39
- tasks = []
40
- results = []
41
- for i in range (limit ):
42
- tasks .append (container .loop .create_task (update_random_record (container )))
43
- yield from asyncio .wait (tasks )
44
- for task in tasks :
45
- results .append (task .result ())
33
+ for i in range (limit ):
34
+ yield from cur .execute ('SELECT id AS "Id", randomnumber AS "RandomNumber" FROM world WHERE id=%(idx)s LIMIT 1' ,
35
+ {'idx' : randint (1 , 10000 )})
36
+ world = yield from cur .fetchone ()
37
+ yield from cur .execute ('UPDATE world SET randomnumber=%(random_number)s WHERE id=%(idx)s' ,
38
+ {'random_number' : randint (1 , 10000 ), 'idx' : world ['Id' ]})
39
+ results .append (world )
46
40
return results
47
41
48
42
@asyncio .coroutine
0 commit comments