Created
April 1, 2018 21:38
-
-
Save fjctp/f5e04a0010d9c365d717f29ca3163747 to your computer and use it in GitHub Desktop.
asyncio and subprocess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import asyncio | |
import logging | |
async def periodic_worker(loop): | |
wait_sec = 1.0 | |
while True: | |
t0 = loop.time() | |
await asyncio.sleep(wait_sec) | |
delta_t = loop.time()-t0 | |
print('Delta T: %.3fs, Error: %.2fms'%(delta_t, (delta_t-wait_sec)*1000)) | |
def main(): | |
logging.getLogger('asyncio').setLevel(logging.WARNING) | |
loop = asyncio.get_event_loop() | |
task = asyncio.ensure_future(periodic_worker(loop), loop=loop) | |
try: | |
loop.run_forever() | |
finally: | |
loop.close() | |
if __name__=='__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import asyncio | |
import logging | |
async def worker(loop, name, sec): | |
await asyncio.sleep(sec) | |
print('{} hello'.format(name)) | |
def main(): | |
logging.getLogger('asyncio').setLevel(logging.WARNING) | |
loop = asyncio.get_event_loop() | |
tasks = [ | |
asyncio.ensure_future(worker(loop, 'A', 3)), | |
asyncio.ensure_future(worker(loop, 'B', 4)), | |
asyncio.ensure_future(worker(loop, 'C', 2)),] | |
try: | |
# loop.run_forever() | |
loop.run_until_complete(asyncio.gather(*tasks)) | |
finally: | |
loop.close() | |
if __name__=='__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from multiprocessing import Pool | |
from time import sleep | |
from time import ctime | |
def f(x): | |
print("%s Sleep for %ssec"%(ctime(), x)) | |
sleep(x) | |
print("%s Sleep for %ssec Done"%(ctime(), x)) | |
if __name__ == "__main__": | |
p = Pool(2) | |
for i in range(10): | |
p.apply_async(f, args=(i,)) | |
print("%s Start"%ctime()) | |
p.close() | |
p.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from multiprocessing import Pool | |
from multiprocessing import current_process | |
def calc(n): | |
sum = 0 | |
for i in range(n): | |
sum = sum + i; | |
print('%s, %s'%(n, sum)) | |
def init(): | |
print("start %s"%current_process().name) | |
if __name__ == '__main__': | |
p = Pool(initializer=init) | |
p.map(calc, [10000,2000,300,400,500,600,700,800,900,1000,1010]) | |
p.close() | |
p.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment