Skip to content

Instantly share code, notes, and snippets.

@fjctp
Created April 1, 2018 21:38
Show Gist options
  • Save fjctp/f5e04a0010d9c365d717f29ca3163747 to your computer and use it in GitHub Desktop.
Save fjctp/f5e04a0010d9c365d717f29ca3163747 to your computer and use it in GitHub Desktop.
asyncio and subprocess
#!/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()
#!/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()
#!/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()
#!/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