是否有可能创建一个非守护进程的python池?我希望一个池能够调用一个内部有另一个池的函数.
我想要这个,因为deamon进程无法创建进程.具体来说,它会导致错误:
AssertionError: daemonic processes are not allowed to have children
Run Code Online (Sandbox Code Playgroud)
例如,考虑function_a具有运行的池的场景,该池具有运行function_b的池function_c.此函数链将失败,因为function_b正在守护进程中运行,并且守护进程无法创建进程.
如何从控制台python应用程序轮询键盘?具体来说,我想在许多其他I/O活动(套接字选择,串行端口访问等)中做类似的事情:
while 1:
# doing amazing pythonic embedded stuff
# ...
# periodically do a non-blocking check to see if
# we are being told to do something else
x = keyboard.read(1000, timeout = 0)
if len(x):
# ok, some key got pressed
# do something
Run Code Online (Sandbox Code Playgroud)
在Windows上执行此操作的正确pythonic方法是什么?此外,Linux的可移植性也不错,但并不是必需的.
我如何让一个线程将一个元组或我选择的任何值返回给Python中的父元素?
我在这里搜索了如何在python中进行线程化,但到目前为止我还没有得到我需要的答案.我对Queue和Threading python类不太熟悉,因此这里出现的一些anwsers对我来说毫无意义.
我想创建一个线程池,我可以给出不同的任务,当它们全部结束时获取结果值并处理它们.到目前为止,我试图这样做,但我无法得到结果.我写的代码是:
from threading import Thread
from Queue import Queue
class Worker(Thread):
"""Thread executing tasks from a given tasks queue"""
def __init__(self, tasks):
Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.result = None
self.start()
def run(self):
while True:
func, args, kargs = self.tasks.get()
try:
self.result = func(*args, **kargs)
except Exception, e:
print e
self.tasks.task_done()
def get_result(self):
return self.result
class ThreadPool:
"""Pool of threads consuming tasks from a queue"""
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
self.results = []
for _ …Run Code Online (Sandbox Code Playgroud) 我可以super()在Python 2.5.6中使用干净的Python 3 语法吗?
也许有某种__future__进口?
可能重复:
从线程返回值
我想得到像这样的一堆服务器的"免费记忆":
def get_mem(servername):
res = os.popen('ssh %s "grep MemFree /proc/meminfo | sed \'s/[^0-9]//g\'"' % servername)
return res.read().strip()
Run Code Online (Sandbox Code Playgroud)
因为这可以是线程我想做类似的事情:
import threading
thread1 = threading.Thread(target=get_mem, args=("server01", ))
thread1.start()
Run Code Online (Sandbox Code Playgroud)
但是现在:如何访问get_mem函数的返回值?我真的需要去一个全功能的方式创建class MemThread(threading.Thread)和覆盖__init__和__run__?
我想找到简单的异步服务器示例.我有很多等待,数据库事务等功能:等等:
def blocking_task(n):
for i in xrange(n):
print i
sleep(1)
return i
Run Code Online (Sandbox Code Playgroud)
我需要在没有阻塞的情况下在分离的进程中运行它 可能吗?
调用了一个返回多个值的外部函数。
def get_name(full_name):
# you code
return first_name, last_name
Run Code Online (Sandbox Code Playgroud)
在简单的函数调用中,我可以得到结果。
from names import get_name
first, last= get_name(full_name)
Run Code Online (Sandbox Code Playgroud)
但是我需要使用线程进行调用以获取第一个和最后一个变量的结果值。我未能使用简单的线程调用。
first, last= Threading.thread(get_name, args= (full_name,)
Run Code Online (Sandbox Code Playgroud)
请帮我获取函数调用的返回值
我试图扩展threading.Timer,以便可以从函数中获取返回值。我使用了该线程的解决方案并进行了修改(因为Timer()已经带有* args和** kwargs了,我认为我不需要__init__再次传递它了)。代码完全像这样:
from threading import Timer
class CustomTimer(Timer):
def __init__(self):
super(CustomTimer, self).__init__()
self._return = None
def run(self):
super(CustomTimer, self).run()
self._return = self._Thread__target(*self._Thread__args, **self._Thread__kwargs)
def join(self):
super(CustomTimer, self).join()
return self._return
Run Code Online (Sandbox Code Playgroud)
然后我在运行主模块时遇到以下错误:
Traceback (most recent call last):
File "main.py", line 43, in <module>
from storage import *
File "/home/mei/tmwAthena/manamarket/storage.py", line 13, in <module>
from utils import ItemDB
File "/home/mei/tmwAthena/manamarket/utils.py", line 142, in <module>
class CustomTimer(Timer):
TypeError: Error when calling the metaclass bases
function() argument 1 must be …Run Code Online (Sandbox Code Playgroud) 我有一个 python 程序,它实现了这样的线程:
class Mythread(threading.Thread):
def __init__(self, name, q):
threading.Thread.__init__(self)
self.name = name
self.q = q
def run(self):
print "Starting %s..." % (self.name)
while True:
## Get data from queue
data = self.q.get()
## do_some_processing with data ###
process_data(data)
## Mark Queue item as done
self.q.task_done()
print "Exiting %s..." % (self.name)
def call_threaded_program():
##Setup the threads. Define threads,queue,locks
threads = []
q = Queue.Queue()
thread_count = n #some number
data_list = [] #some data list containing data
##Create Threads
for …Run Code Online (Sandbox Code Playgroud) python ×10
asynchronous ×1
blocking ×1
console ×1
exit-code ×1
hung ×1
interrupt ×1
keyboard ×1
memory ×1
nonblocking ×1
pool ×1
python-2.5 ×1
python-2.x ×1
python-3.x ×1
queue ×1
return-value ×1
super ×1
tornado ×1