python-3.4

When using asyncio, how do you allow all running tasks to finish before shutting down the event loop

五迷三道 提交于 2019-11-28 18:26:08
问题 I have the following code: @asyncio.coroutine def do_something_periodically(): while True: asyncio.async(my_expensive_operation()) yield from asyncio.sleep(my_interval) if shutdown_flag_is_set: print("Shutting down") break I run this function until complete. The problem occurs when shutdown is set - the function completes and any pending tasks are never run. (You see this as an error task: <Task pending coro=<report() running at script.py:33> wait_for=<Future pending cb=[Task._wakeup()]>> ).

How to properly create and run concurrent tasks using python's asyncio module?

若如初见. 提交于 2019-11-28 15:19:19
I am trying to properly understand and implement two concurrently running Task objects using Python 3's relatively new asyncio module. In a nutshell, asyncio seems designed to handle asynchronous processes and concurrent Task execution over an event loop. It promotes the use of await (applied in async functions) as a callback-free way to wait for and use a result, without blocking the event loop. (Futures and callbacks are still a viable alternative.) It also provides the asyncio.Task() class, a specialized subclass of Future designed to wrap coroutines. Preferably invoked by using the asyncio

Method for having animated movement for canvas objects python

回眸只為那壹抹淺笑 提交于 2019-11-28 14:05:47
I have been trying to learn how move canvas items from google, however the method shown most places doesnt seem to work for me as intended. right now i am just trying to get a ball move from one side of the screen to the other over the period of 1 second from tkinter import * root = Tk() c = Canvas(root, width = 200, height = 100) c.pack() ball = c.create_oval(0, 25, 50, 75) for i in range(25): c.move(ball, 6, 0) root.after(40) root.mainloop() when run, this seems to move the ball before opening the window, however if i call upon mainloop first, the window opens but the ball doesn't move.

Python threading self._stop() 'Event' object is not callable

久未见 提交于 2019-11-28 13:57:11
Trying a "stoppable" thread from https://stackoverflow.com/a/325528/1619432 like so: import sys import threading import time import logging class StoppableThread(threading.Thread): """Thread class with a stop() method. The thread itself has to check regularly for the stopped() condition.""" def __init__(self): print( "base init", file=sys.stderr ) super(StoppableThread, self).__init__() self._stop = threading.Event() def stop(self): print( "base stop()", file=sys.stderr ) self._stop.set() def stopped(self): return self._stop.is_set() class datalogger(StoppableThread): """ """ import time def _

how to cleanly uninstall my python packages with pip3 or any other way?

限于喜欢 提交于 2019-11-28 11:15:24
this is my setup.py file for installing my python program, after the installation using python3 setup.py install an entry to my program was created named testmain , when i did pip3 freeze it showed abc==0.1 in its output ,so i uninstalled it using pip3 with pip3 uninstall abc , though the packages were uninstalled but there still existed the entry testmain on my path , is there a way that pip3 also removes this entry during the uninstall or any other way that i can cleanly uninstall my programs under same scenario ? from setuptools import setup setup(name='abc', version='0.1', description=

from . import _methods ImportError: cannot import name '_methods' in cx-freeze python

余生颓废 提交于 2019-11-28 10:52:39
exe build successfully by using cx-freeze. But it shows the following error when I execute the exe file: from . import _methods ImportError: cannot import name '_methods' Rodolfo This question was already answer here: Why am I getting this ImportError? but for the sake of completeness here is the answer for this specific case: cx_freeze is not importing the optional module _method , so you have to tell him explicitly to do it. additional_mods = ['numpy.core._methods', 'numpy.lib.format'] setup(name='xyz', version='0.4', description='xyz script', options = {'build_exe': {'includes': additional

Why does the asyncio's event loop suppress the KeyboardInterrupt on Windows?

家住魔仙堡 提交于 2019-11-28 08:59:00
I have this really small test program which does nothing apart from a executing an asyncio event loop: import asyncio asyncio.get_event_loop().run_forever() When I run this program on Linux and press Ctrl + C , the program will terminate correctly with a KeyboardInterrupt exception. On Windows pressing Ctrl + C does nothing (tested with Python 3.4.2). A simple inifinite loop with time.sleep() raises the KeyboardInterrupt correctly even on Windows: import time while True: time.sleep(3600) Why does the asyncio's event loop suppress the KeyboardInterrupt on Windows? This is a bug, sure. See issue

How to lock a directory between python processes in linux?

早过忘川 提交于 2019-11-28 08:06:49
问题 I have two (or more) python processes running and want to create a concept similar to an exclusion mutex for a shared resource. The 'shared resource' in this case is a directory. How might I most easily/standardly/etc implement a mutex? A hidden .lock file that each process agrees to check and, if exists, appends their PID as a new row and then pops their PID when they have access to the file? I basically just want to clear a directory and make sure no other process tries to read or write to

Making a collatz program automate the boring stuff

徘徊边缘 提交于 2019-11-28 07:37:16
I'm trying to write a Collatz program using the guidelines from a project found at the end of chapter 3 of Automate the Boring Stuff with Python. I'm using python 3.4.0 . Following is the project outline: Write a function named collatz() that has one parameter named number. If the number is even, then collatz() should print number // 2 and return this value. If the number is odd, then collatz() should print and return 3 * number + 1 . Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1 . The output of

Python TypeError: 'float' object cannot be interpreted as an integer

ぃ、小莉子 提交于 2019-11-28 06:39:45
问题 My code: for i in range( 3.3, 5 ): print( i ) The above code have to print: 3.300000 4.300000 but the interpreter of Python 3.4.0 printed the following error: TypeError: 'float' object cannot be interpreted as an integer 回答1: range() works with integers not floats, but you can build your own range generator which will do what you want: def frange(start, stop, step=1): i = start while i < stop: yield i i += step for i in frange(3.3, 5) will give you the desired result. Note though, that frange