Python - decorators based execution - python

I am trying to execute all the functions with common decorator without calling all the functions. For example
#run
#v1
def test1():
#do something
#run
#v1
#v2
def test2():
#do something
#run
#v2
def test3():
#do something
#run
def test4():
#do something
I want to executes the test functions based on the decorators like #run executes all 4 tests. #v1 executes only first two. How can I do that? Any guidance will be helpful.

You could probably use the decorator to "register" your functions in a list:
_to_run = [] # list of functions to run
def run(func):
_to_run.append(func) # add the decorated function to the list
return func
#run
def test1():
print('test1')
return 1
#run
def test2():
print('test2')
def test3():
print('test3')
if __name__ == '__main__':
for test in _to_run: # iterate over registered functions
x = test()
print('Returned:', x)
On the other hand, you could as well create this list explicitly, without decorators.

Related

retrieve result of async function inside of another async function

I'm facing this problem with async function
my_class.py
class cl:
async def foo():
return
async def bar(foo):
#some code here
return result
main.py
from my_class import cl
import asyncio
c = cl()
r = asyncio.run(c.foo)
x = cl.bar(r)
s = asyncio.run(x)
How can I retrieve the return of foo() to the function bar() because now I get this error :
ValueError: The future belongs to a different loop than the one specified as the loop argument
THANKS!!
Futures in Python imply that the result is available in the future, as the asynchronous function has not "finished" executing. I assume you will want to wait for foo to "finish" running in bar before using its value. This should do it:
import asyncio
class cl:
async def foo(self):
return
async def bar(self):
value = await self.foo() # waits for foo, then gets its value
#some code here
return result
c = cl()
x = c.bar()
s = asyncio.run(x)
Note: I changed some minor syntax so this code snippet can execute as-is

The execution order inside a corutine?

Yield from coroutine vs yield from task
In this link, there is an example give by #dano that:
import asyncio
#asyncio.coroutine
def test1():
print("in test1")
#asyncio.coroutine
def dummy():
yield from asyncio.sleep(1)
print("dummy ran")
#asyncio.coroutine
def main():
test1()
yield from dummy()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
The output is only
dummy ran
I can't add comment to that directly, so I have to ask a new question here;
(1) why the test1() isn't executed in order in such corutine function.
Whether corutine can only use the following two ways?
yield from cor()
asyncio.async(cor())
Where did test1() go?
(2) There is also some other problems in understanding the differnence of the followng two methods to use corutine() function. Are they the same?
yield from asyncio.async(cor())
asyncio.async(cor())
I use the following code to explain:
import random
import datetime
global a,b,c
import asyncio
a = [random.randint(0, 1<<256) for i in range(500000)]
b= list(a)
c= list(a)
#asyncio.coroutine
def test1():
global b
b.sort(reverse=True)
print("in test1")
#asyncio.coroutine
def test2():
global c
c.sort(reverse=True)
print("in test2")
#asyncio.coroutine
def dummy():
yield from asyncio.sleep(1)
print("dummy ran")
#asyncio.coroutine
def test_cor():
for i in asyncio.sleep(1):
yield i
#asyncio.coroutine
def main():
test1()
print("hhhh_______________________")
asyncio.async(test1())
asyncio.async(test2())
print("hhhh_______________________")
print("hhh")
asyncio.async(dummy())
yield from test_cor()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print("hhhhhhh")
However the output is
hhhh_______________________
hhhh_______________________
hhh
in test1
in test2
hhhhhhh
It even didn't execute the dummy() function !
And I use
#asyncio.coroutine
def test2():
# global c
# c.sort(reverse=True)
print("in test2")
(3) without sorting and I think test2 should run faster so that test1 is output after test2. However, the output didn't change. I don't know why.
(4)And I also tried to remove sorting for both test1() and test2(), then amazingly, dummy() runs and output the following. Why ??
hhhh_______________________
hhhh_______________________
hhh
in test1
in test2
dummy ran
hhhhhhh
I don't know how these things happens....I am relly bewilerded.

Python make each lock in multithread

I'm considering how to make several locks for each thread.
I have 3 threads right now.
A : main thread(data sending)
B : data receiving thread
C : data sending every 2 sec thread
I don't want to stop B(receiving thread) except sending time.
How can I use Lock between A,B and between A,C easily!!...
class A:
def __init__():
self._A_B_lock = RLock()
self._A_C_lock = RLock()
self._B = threading.Thread(target=B_receiving_thread, args=(self._A_B_lock,) ... ).start()
self._C = threading.Thread(target=C_sending_2sec_thread, args=(self._A_C_lock,) ... ).start()
def sending():
with A_B_lock:
sending_data()
def B_receiving_thread(self,A_B_lock):
while(1):
with A_B_lock:
receiving_data()
#do something
def C_sending_2sec_thread(self,A_C_lock):
while(1):
with A_C_lock:
self.sending()
# actually I want to make decorator with A_C_lock, I have so many functions.
def so_many_functions():
with self.A_C_lock:
#do important thing
This code doesn’t work..
A decorator is a good idea. You could use this
def decorator(*locks):
def _decorator(func):
def inner_function(*args, **kwargs):
for lock in locks:
lock.acquire()
value = func(*args, **kwargs)
for lock in locks:
lock.release()
return value
return inner_function
return _decorator
And then you decorate each function, and pass as parameter all the locks that that function will need to make his job without interfeering others. Like this,
lock1 = threading.Lock()
lock2 = threading.Lock()
#decorator(lock1, lock2)
def f1(word):
for char in word:
print(char)
'''DO STUFF'''
#decorator(lock1, lock2)
def f2(word):
for char in word:
print(char)
'''
DO STUFF
'''
t1 = threading.Thread(target=f1, args=('Hello ',))
t2 = threading.Thread(target=f2, args=('world',))
t1.start()
t2.start()
Thats just a dummy example, but you can easily apply it to your code. Good thing about it, is that you can easily choose wich locks you want to use for each different function.
Hope it helps

How to set up the thread.Timer without a function?

I am working on my code to set up the timer. I want to set up the Timer without creating the function.
Here is what I use:
def hello(self):
print "hello, world"
self.getControl(MyPlayer.Control_EPG_ID).setVisible(False)
def onAction(self, action):
if action.getId() == ACTION_MOVE_DOWN:
t = threading.Timer(5.0, self.hello)
t.start()
I want to make it to show something is like this:
t = threading.Timer(5.0)
t.start()
self.getControl(MyPlayer.Control_EPG_ID).setVisible(False)
when I try it, it give me an error. Do you know how I can set up the timer without create a function? if so how?
You should be able to nest hello() inside of onAction() like this. It will use the self argument passed to the outer function:
def onAction(self, action):
def hello():
print "hello, world"
self.getControl(MyPlayer.Control_EPG_ID).setVisible(False)
if action.getId() == ACTION_MOVE_DOWN:
t = threading.Timer(5.0, hello)
t.start()
Why not like this:
import time
time.sleep(5) # delays for 5 seconds

How to get the result of multiprocessing.Pool.apply_async

I want to get the result of the function run by Pool.apply_async in Python.
How to assign the result to a variable in the parent process? I tried to use callback but it seems complicated.
The solution is very simple:
import multiprocessing
def func():
return 2**3**4
p = multiprocessing.Pool()
result = p.apply_async(func).get()
print(result)
Since Pool.apply_async() returns an AsyncResult, you can simply get the result from the AsyncResult.get() method.
Hope this helps!
Well an easy way would be to have a helper class like this:
class Result():
def __init__(self):
self.val = None
def update_result(self, val):
self.val = val
result = Result()
def f(x):
return x*x
pool.apply_async(f, (10,), callback=result.update_result)
When the thread runs and calculates the result it will call your callback which will update the result.val.
In any case you need to check that the thread has finished.

Categories

Resources