How can I use functools.singledispatch with instance methods?

Update: As of Python 3.8, functools.singledispatchmethod allows single dispatch on methods, classmethods, abstractmethods, and staticmethods. For older Python versions, see the rest of this answer. Looking at the source for singledispatch, we can see that the decorator returns a function wrapper(), which selects a function to call from those registered based on the type of … Read more

Please explain “Task was destroyed but it is pending!” after cancelling tasks

The problem comes from closing the loop immediately after cancelling the tasks. As the cancel() docs state “This arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop.” Take this snippet of code: import asyncio import signal async def pending_doom(): await asyncio.sleep(2) print(“>> Cancelling tasks now”) … Read more

What’s the correct way to clean up after an interrupted event loop?

When you CTRL+C, the event loop gets stopped, so your calls to t.cancel() don’t actually take effect. For the tasks to be cancelled, you need to start the loop back up again. Here’s how you can handle it: import asyncio @asyncio.coroutine def shleepy_time(seconds): print(“Shleeping for {s} seconds…”.format(s=seconds)) yield from asyncio.sleep(seconds) if __name__ == ‘__main__’: loop … Read more

OSError: [WinError 193] %1 is not a valid Win32 application

The error is pretty clear. The file hello.py is not an executable file. You need to specify the executable: subprocess.call([‘python.exe’, ‘hello.py’, ‘htmlfilename.htm’]) You’ll need python.exe to be visible on the search path, or you could pass the full path to the executable file that is running the calling script: import sys subprocess.call([sys.executable, ‘hello.py’, ‘htmlfilename.htm’])

Getting values from functions that run as asyncio tasks

The coroutines work as is. Just use the returned value from loop.run_until_complete() and call asyncio.gather() to collect multiple results: #!/usr/bin/env python3 import asyncio @asyncio.coroutine def func_normal(): print(‘A’) yield from asyncio.sleep(5) print(‘B’) return ‘saad’ @asyncio.coroutine def func_infinite(): for i in range(10): print(“–%d” % i) return ‘saad2’ loop = asyncio.get_event_loop() tasks = func_normal(), func_infinite() a, b = … Read more

How to install pip in CentOS 7?

The easiest way I’ve found to install pip3 (for python3.x packages) on CentOS 7 is: $ sudo yum install python34-setuptools $ sudo easy_install-3.4 pip You’ll need to have the EPEL repository enabled before hand, of course. You should now be able to run commands like the following to install packages for python3.x: $ pip3 install … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)