initial commit
commit
762804fb70
@ -0,0 +1,70 @@
|
||||
# virtualenv
|
||||
env
|
||||
venv
|
||||
|
||||
# Pycharm
|
||||
.idea
|
||||
|
||||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
env/
|
||||
pypyenv/
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*,cover
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# Redis
|
||||
/dump.rdb
|
||||
|
||||
# Project
|
||||
config.py
|
@ -0,0 +1,37 @@
|
||||
import asyncio
|
||||
|
||||
|
||||
async def find_divisibles(inrange, div_by):
|
||||
"""Really this is a bulk task"""
|
||||
print("finding nums in range {} divisible by {}".format(inrange, div_by))
|
||||
located = []
|
||||
for i in range(inrange):
|
||||
if i % div_by == 0:
|
||||
located.append(i)
|
||||
if i % 50000 == 0:
|
||||
await asyncio.sleep(0.001)
|
||||
|
||||
|
||||
print("Done w/ nums in range {} divisble by {}".format(inrange, div_by))
|
||||
return located
|
||||
|
||||
|
||||
async def main():
|
||||
divs1 = loop.create_task(find_divisibles(50800000, 34113))
|
||||
divs2 = loop.create_task(find_divisibles(100052, 3210))
|
||||
divs3 = loop.create_task(find_divisibles(500, 3))
|
||||
await asyncio.wait([divs1, divs2, divs3])
|
||||
return divs1, divs2, divs3
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
# returns a Task object, not the result objects themselves. You have to access the Task result
|
||||
# to see the output
|
||||
d1, d2, d3 = loop.run_until_complete(main())
|
||||
print(type(d1))
|
||||
print(d1.result())
|
||||
except Exception as e:
|
||||
pass
|
||||
finally:
|
||||
loop.close()
|
@ -0,0 +1,42 @@
|
||||
import asyncio
|
||||
import itertools
|
||||
import sys
|
||||
|
||||
|
||||
async def spin(msg):
|
||||
write, flush = sys.stdout.write, sys.stdout.flush
|
||||
for char in itertools.cycle('|/-\\'):
|
||||
status = f"{char} {msg}"
|
||||
write(status)
|
||||
flush()
|
||||
write('\x08' * len(status)) # backspace out the line
|
||||
try:
|
||||
await asyncio.sleep(.1)
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
write(' ' * len(status) + '\x08' * len(status))
|
||||
|
||||
|
||||
async def slow_function():
|
||||
await asyncio.sleep(3)
|
||||
return 42
|
||||
|
||||
|
||||
async def supervisor():
|
||||
spinner = loop.create_task(spin('Thinking!'))
|
||||
print('Spinner Object:', spinner)
|
||||
result = await slow_function()
|
||||
spinner.cancel()
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
# returns a Task object, not the result objects themselves. You have to access the Task result
|
||||
# to see the output
|
||||
result = loop.run_until_complete(supervisor())
|
||||
except Exception as e:
|
||||
pass
|
||||
finally:
|
||||
loop.close()
|
@ -0,0 +1,32 @@
|
||||
"""
|
||||
Here is a trivial example of us awaiting the result of another coroutine. We did this by using the await
|
||||
keyword before calling it. The await keyword gives control back to the event loop and registers add_42(23) function
|
||||
and arguments call to the Task Queue allowing the event loop to schedule other tasks. In this case this
|
||||
single function is the only Task on the Queue so it will be picked up and executed. Once executed the result is
|
||||
returned to hello_async allowing it to be resumed by the event loop scheduler.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def add_42(number):
|
||||
if not isinstance(number, int):
|
||||
raise ValueError("You need to supply a number to this bitch")
|
||||
print("Adding 42")
|
||||
return 42 + number
|
||||
|
||||
|
||||
async def hello_async():
|
||||
print("Hello async!")
|
||||
co_result = await add_42(23)
|
||||
return co_result
|
||||
|
||||
|
||||
event_loop = asyncio.get_event_loop()
|
||||
|
||||
try:
|
||||
print("Entering event loop")
|
||||
result = event_loop.run_until_complete(hello_async())
|
||||
print(result)
|
||||
finally:
|
||||
event_loop.close()
|
@ -0,0 +1,24 @@
|
||||
"""
|
||||
Once asyncio has created an event loop,an application registers the functions to call back when a specific event happens:
|
||||
as time passes, a file descriptor is ready to be read,or a socket is ready to be written. That type of function is
|
||||
called a coroutine. It is a particular type of function that can give back control to the caller so that the event loop
|
||||
can continue running.
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
# Adding the async keyword makes this a coroutine object
|
||||
async def hello_world():
|
||||
print('Hello world')
|
||||
return 42
|
||||
|
||||
|
||||
hello_world_coroutine = hello_world()
|
||||
print(hello_world_coroutine)
|
||||
|
||||
event_loop = asyncio.get_event_loop()
|
||||
try:
|
||||
print('Entering the event loop')
|
||||
result = event_loop.run_until_complete(hello_world_coroutine)
|
||||
print(result)
|
||||
finally:
|
||||
event_loop.close()
|
@ -0,0 +1,21 @@
|
||||
import asyncio
|
||||
|
||||
|
||||
async def hello_async():
|
||||
print('Hello Async!')
|
||||
|
||||
|
||||
async def hello_python():
|
||||
print('Hello Python')
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
event_loop = asyncio.get_event_loop()
|
||||
try:
|
||||
result = event_loop.run_until_complete(asyncio.gather(
|
||||
hello_async(),
|
||||
hello_python(),
|
||||
hello_python(),
|
||||
))
|
||||
print(result)
|
||||
finally:
|
||||
event_loop.close()
|
@ -0,0 +1,20 @@
|
||||
"""
|
||||
Here we have turned was was a synchronous call into an async call with the use of the select module.
|
||||
select is old however and OSs have newer system calls. Asyncio abstracts the system calls out, and
|
||||
gives us primitives to build better async code.
|
||||
|
||||
"""
|
||||
|
||||
import select
|
||||
import socket
|
||||
|
||||
s = socket.create_connection(("httpbin.org", 80))
|
||||
s.send(b"GET /delay/5 HTTP/1.1\r\nHost: httpbin.org\r\n\r\n")
|
||||
s.setblocking(False)
|
||||
|
||||
while True:
|
||||
ready_to_read, ready_to_write, in_error = select.select([s], [], [])
|
||||
if s in ready_to_read:
|
||||
buf = s.recv(1024)
|
||||
print(buf)
|
||||
break
|
Loading…
Reference in New Issue