async fuction

Discussion in 'Python' started by aleksey@silk.bz, Jan 12, 2013.

  1. Guest

    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)
     
    , Jan 12, 2013
    #1
    1. Advertising

  2. MRAB Guest

    On 2013-01-12 04:43, 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.
     
    MRAB, Jan 12, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    3
    Views:
    373
    Chris Botha
    Nov 16, 2005
  2. somez72
    Replies:
    4
    Views:
    4,369
    Roedy Green
    May 3, 2004
  3. MiniDisc_2k2
    Replies:
    2
    Views:
    393
    MiniDisc_2k2
    Jul 3, 2003
  4. orion30
    Replies:
    2
    Views:
    316
    Alf P. Steinbach
    Jul 14, 2003
  5. Steven
    Replies:
    0
    Views:
    377
    Steven
    Nov 30, 2005
Loading...

Share This Page