Skip to main content
deleted 758 characters in body
Source Link
vaskrneup
  • 111
  • 1
  • 5

You can run code asynchronously and provide callback once the code is executed, thisDISCLAMER: Following code creates different threads for each function.

This might not be suitableuseful for each casesome of the cases as it is simpler to use. But know that it is not async but gives illusion of async using multiple threads, even though decorator suggests that.

import asyncio import requests class RunAsyncWithCallback: def __init__(self, _loop=None): self._loop = _loop or asyncio.get_event_loop() def submit_func(self, func, callback, *args, **kwargs): def __exec(): callback(func(*args, **kwargs)) self._loop.run_in_executor(None, __exec) def set_event_loop(self, loop): self._loop = loop # Callback must accept the values returned by provided function !! def _callback(*args): print(args) def get(url): return requests.get(url) if __name__ == '__main__': runner = RunAsyncWithCallback() for url in ["https://google.com", "https://facebook.com", "https://apple.com", "https://netflix.com"]: runner.submit_func(get, _callback, url) print("Non Blocking code ran !!") 

You can also create a decorator for running code asynchronouslyTo make any function non blocking, simply copy the decorator and providedecorate any function with a callback oncefunction as parameter. The callback function will receive the data returned from the function completes execution !!.

import asyncio import requests def run_async(callback): def inner(func): def wrapper(*args, **kwargs): def __exec(): out = func(*args, **kwargs) callback(out) return out return asyncio.get_event_loop().run_in_executor(None, __exec) return wrapper return inner def _callback(*args): print(args) # Must provide a callback function, callback func will be executed after the func completes execution !! @run_async(_callback) def get(url): return requests.get(url) get("https://google.com") print("Non blocking code ran !!") 

You can run code asynchronously and provide callback once the code is executed, this might not be suitable for each case.

import asyncio import requests class RunAsyncWithCallback: def __init__(self, _loop=None): self._loop = _loop or asyncio.get_event_loop() def submit_func(self, func, callback, *args, **kwargs): def __exec(): callback(func(*args, **kwargs)) self._loop.run_in_executor(None, __exec) def set_event_loop(self, loop): self._loop = loop # Callback must accept the values returned by provided function !! def _callback(*args): print(args) def get(url): return requests.get(url) if __name__ == '__main__': runner = RunAsyncWithCallback() for url in ["https://google.com", "https://facebook.com", "https://apple.com", "https://netflix.com"]: runner.submit_func(get, _callback, url) print("Non Blocking code ran !!") 

You can also create a decorator for running code asynchronously, and provide a callback once the function completes execution !!

import asyncio import requests def run_async(callback): def inner(func): def wrapper(*args, **kwargs): def __exec(): out = func(*args, **kwargs) callback(out) return out return asyncio.get_event_loop().run_in_executor(None, __exec) return wrapper return inner def _callback(*args): print(args) # Must provide a callback function, callback func will be executed after the func completes execution !! @run_async(_callback) def get(url): return requests.get(url) get("https://google.com") print("Non blocking code ran !!") 

DISCLAMER: Following code creates different threads for each function.

This might be useful for some of the cases as it is simpler to use. But know that it is not async but gives illusion of async using multiple threads, even though decorator suggests that.

To make any function non blocking, simply copy the decorator and decorate any function with a callback function as parameter. The callback function will receive the data returned from the function.

import asyncio import requests def run_async(callback): def inner(func): def wrapper(*args, **kwargs): def __exec(): out = func(*args, **kwargs) callback(out) return out return asyncio.get_event_loop().run_in_executor(None, __exec) return wrapper return inner def _callback(*args): print(args) # Must provide a callback function, callback func will be executed after the func completes execution !! @run_async(_callback) def get(url): return requests.get(url) get("https://google.com") print("Non blocking code ran !!") 
added 926 characters in body
Source Link
vaskrneup
  • 111
  • 1
  • 5

You can run code asynchronously and provide callback once the code is executed, this might not be suitable for each case.

import asyncio import requests class RunAsyncWithCallback: def __init__(self, _loop=None): self._loop = _loop or asyncio.get_event_loop() def submit_func(self, func, callback, *args, **kwargs): def __exec(): callback(func(*args, **kwargs)) self._loop.run_in_executor(None, __exec) def set_event_loop(self, loop): self._loop = loop # Callback must accept the values returned by provided function !! def _callback(*args): print(args) def get(url): return requests.get(url) if __name__ == '__main__': runner = RunAsyncWithCallback() for url in ["https://google.com", "https://facebook.com", "https://apple.com", "https://netflix.com"]: runner.submit_func(get, _callback, url) print("Non Blocking code ran !!") 

You can also create a decorator for running code asynchronously, and provide a callback once the function completes execution !!

import asyncio import requests def run_async(callback): def inner(func): def wrapper(*args, **kwargs): def __exec(): out = func(*args, **kwargs) callback(out) return out return asyncio.get_event_loop().run_in_executor(None, __exec) return wrapper return inner def _callback(*args): print(args) # Must provide a callback function, callback func will be executed after the func completes execution !! @run_async(_callback) def get(url): return requests.get(url) get("https://google.com") print("Non blocking code ran !!") 

You can run code asynchronously and provide callback once the code is executed, this might not be suitable for each case.

import asyncio import requests class RunAsyncWithCallback: def __init__(self, _loop=None): self._loop = _loop or asyncio.get_event_loop() def submit_func(self, func, callback, *args, **kwargs): def __exec(): callback(func(*args, **kwargs)) self._loop.run_in_executor(None, __exec) def set_event_loop(self, loop): self._loop = loop # Callback must accept the values returned by provided function !! def _callback(*args): print(args) def get(url): return requests.get(url) if __name__ == '__main__': runner = RunAsyncWithCallback() for url in ["https://google.com", "https://facebook.com", "https://apple.com", "https://netflix.com"]: runner.submit_func(get, _callback, url) print("Non Blocking code ran !!") 

You can run code asynchronously and provide callback once the code is executed, this might not be suitable for each case.

import asyncio import requests class RunAsyncWithCallback: def __init__(self, _loop=None): self._loop = _loop or asyncio.get_event_loop() def submit_func(self, func, callback, *args, **kwargs): def __exec(): callback(func(*args, **kwargs)) self._loop.run_in_executor(None, __exec) def set_event_loop(self, loop): self._loop = loop # Callback must accept the values returned by provided function !! def _callback(*args): print(args) def get(url): return requests.get(url) if __name__ == '__main__': runner = RunAsyncWithCallback() for url in ["https://google.com", "https://facebook.com", "https://apple.com", "https://netflix.com"]: runner.submit_func(get, _callback, url) print("Non Blocking code ran !!") 

You can also create a decorator for running code asynchronously, and provide a callback once the function completes execution !!

import asyncio import requests def run_async(callback): def inner(func): def wrapper(*args, **kwargs): def __exec(): out = func(*args, **kwargs) callback(out) return out return asyncio.get_event_loop().run_in_executor(None, __exec) return wrapper return inner def _callback(*args): print(args) # Must provide a callback function, callback func will be executed after the func completes execution !! @run_async(_callback) def get(url): return requests.get(url) get("https://google.com") print("Non blocking code ran !!") 
Source Link
vaskrneup
  • 111
  • 1
  • 5

You can run code asynchronously and provide callback once the code is executed, this might not be suitable for each case.

import asyncio import requests class RunAsyncWithCallback: def __init__(self, _loop=None): self._loop = _loop or asyncio.get_event_loop() def submit_func(self, func, callback, *args, **kwargs): def __exec(): callback(func(*args, **kwargs)) self._loop.run_in_executor(None, __exec) def set_event_loop(self, loop): self._loop = loop # Callback must accept the values returned by provided function !! def _callback(*args): print(args) def get(url): return requests.get(url) if __name__ == '__main__': runner = RunAsyncWithCallback() for url in ["https://google.com", "https://facebook.com", "https://apple.com", "https://netflix.com"]: runner.submit_func(get, _callback, url) print("Non Blocking code ran !!")