Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   async fuction (http://www.velocityreviews.com/forums/t956448-async-fuction.html)

aleksey@silk.bz 01-12-2013 04:43 AM

async fuction
 
Hello.

Can someone help me to resolv error.

code:


import threading

class TimeoutError(RuntimeError):
pass

class AsyncCall(object):
def __init__(self, fnc, callback = None):
self.Callable = fnc
self.Callback = callback

def __call__(self, *args, **kwargs):
self.Thread = threading.Thread(target = self.run, name = self.Callable.__name__, args = args, kwargs = kwargs)
self.Thread.start()
return self

def wait(self, timeout = None):
self.Thread.join(timeout)
if self.Thread.isAlive():
raise TimeoutError()
else:
return self.Result

def run(self, *args, **kwargs):
self.Result = self.Callable(*args, **kwargs)
if self.Callback:
self.Callback(self.Result)

class AsyncMethod(object):
def __init__(self, fnc, callback=None):
self.Callable = fnc
self.Callback = callback

def __call__(self, *args, **kwargs):
return AsyncCall(self.Callable, self.Callback)(*args, **kwargs)

def Async(fnc = None, callback = None):
if fnc == None:
def AddAsyncCallback(fnc):
return AsyncMethod(fnc, callback)
return AddAsyncCallback
else:
return AsyncMethod(fnc, callback)








@Async
def fnc(pi, pp):

print "fnc-"
i=pi
while ( i < 10000000 ) :
i=i+1
print "fnc+"
pass

@Async
def fnc1(pp):
print "fnc1-",pp


@Async
def fnc2():
print "fnc2-"
i=0
while ( i < 100000 ) :
i=i+1
print "fnc2+"
pass

fnc(i=0,pp="123123")
fnc1()


error:

Exception in thread fnc1:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "C:/Users/rootiks/YandexDisk/py/myftpbackup/asynclib.py", line 26, in run
self.Result = self.Callable(*args, **kwargs)
TypeError: fnc1() takes exactly 1 argument (0 given)


MRAB 01-12-2013 06:01 PM

Re: async fuction
 
On 2013-01-12 04:43, aleksey@silk.bz wrote:
> Hello.
>
> Can someone help me to resolv error.
>
> code:
>

[snip]
>
> @Async
> def fnc(pi, pp):
>
> print "fnc-"
> i=pi
> while ( i < 10000000 ) :
> i=i+1
> print "fnc+"
> pass
>
> @Async
> def fnc1(pp):
> print "fnc1-",pp
>
>
> @Async
> def fnc2():
> print "fnc2-"
> i=0
> while ( i < 100000 ) :
> i=i+1
> print "fnc2+"
> pass
>
> fnc(i=0,pp="123123")
> fnc1()
>
>
> error:
>
> Exception in thread fnc1:
> Traceback (most recent call last):
> File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
> self.run()
> File "C:\Python27\lib\threading.py", line 504, in run
> self.__target(*self.__args, **self.__kwargs)
> File "C:/Users/rootiks/YandexDisk/py/myftpbackup/asynclib.py", line 26, in run
> self.Result = self.Callable(*args, **kwargs)
> TypeError: fnc1() takes exactly 1 argument (0 given)
>

1. You're calling function 'fnc' with keyword arguments 'i' and 'pp';
it's expecting 'pi' and 'pp'.

2. You're calling function 'fnc1' with no arguments; it's expecting one
argument.



All times are GMT. The time now is 02:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57