Skip to content

Instantly share code, notes, and snippets.

@mzfr
Last active May 25, 2018 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzfr/7f229b78e4eeef97a28ba1975ac646af to your computer and use it in GitHub Desktop.
Save mzfr/7f229b78e4eeef97a28ba1975ac646af to your computer and use it in GitHub Desktop.
task.py
"""
Reading and Writing file in queue using asyncio
To run: python3 task.py <input_file> <output_file>
It was a task for my GSoC'18 selection under Honeynet(I failed!!)
"""
import asyncio
import sys
async def read(queue, reading_file_path):
"""Read a file a puts it into a queue"""
with open(reading_file_path, 'r') as file:
for line in file:
await queue.put(line)
# Finished reading
await queue.put(None)
async def write(queue, writing_file_path):
"""write to another file using data present in queue"""
with open(writing_file_path, 'w') as file:
while True:
line = await queue.get()
# The reader emits None when entire file has been read
if line is None:
break
file.write(line)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
queue = asyncio.Queue(loop=loop)
reading_file_path = sys.argv[1]
writing_file_path = sys.argv[2]
reading = read(queue, reading_file_path)
writing = write(queue, writing_file_path)
loop.run_until_complete(asyncio.gather(reading, writing))
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment