collections.deque 我一直在此处和此处阅读有关 CPython 的线程安全性的内容。我知道.append()和.popleft()操作在线程之间可以很好地配合,就像.appendleft()和一样.pop()。
.popleft()我的问题是:和之间同样适用吗.clear()?或者如果失败的话,在.popleft()和之间.pop()?我的情况是,我有一个消费者线程,称之为 A,不断地.popleft()从 a 获取项目deque d,还有一个生产者线程 B,将.append()它们放在右侧。在某些时候,我希望线程 B 能够说“取消所有待处理项目”。如果我这样做
d.clear()
Run Code Online (Sandbox Code Playgroud)
由于与线程 A 的操作冲突,它是否可能导致未定义的行为.popleft()?如果我说:
while d:
try: d.pop()
except: break
Run Code Online (Sandbox Code Playgroud)
反而?
我碰巧在这里使用 Python 绑定,但我怀疑这个问题和最终的答案不是 Python 特定的。
在 Windows 10 上,使用Python 绑定到 Windows 的核心音频库,特别是通过pycaw.AudioUtilities.GetAllSessions(),我可以看到有一个名称'conhost.exe'和 pid 为 11512的会话,并且通过基本的猜测/实验,我可以看到这是 (a) 管理的会话我当前进程的音频和 (b) 对应于 Windows 10 音量混合器中的“控制台窗口主机”滑块。这是我想从我的程序中操作的,并pycaw提供绑定来做到这一点。
到现在为止还挺好。我的问题是我没有看到任何从当前进程 ID 向后工作的方法,因此在下次启动时,我的程序可以询问“哪个进程对应于管理我的音频输出的会话?” 我天真地希望该会话 pid 可能出现在当前进程的祖先中,但它没有:
import os, psutil
psutil.Process(os.getpid())
# prints: psutil.Process(pid=3628, name='python.exe', started='14:56:41')
psutil.Process(os.getpid()).parent()
# prints: psutil.Process(pid=16676, name='cmd.exe', started='13:00:57')
psutil.Process(os.getpid()).parent().parent()
# prints: psutil.Process(pid=1356, name='explorer.exe', started='12:21:26')
psutil.Process(os.getpid()).parent().parent().parent()
# prints nothing. It's `None`. We've reached the top.
Run Code Online (Sandbox Code Playgroud)
我应该如何查询控制当前进程的任何音频会话的 pid?(我认为会话不会总是被命名——'conhost.exe有时我会pythonw.exe在没有控制台的情况下运行相同的代码。)
我有一个字符串,s = 'sdfjoiweng%@$foo$fsoifjoi'和我想替换'foo'用'bar'.
我试过re.sub(r'\bfoo\b', 'bar', s)和re.sub(r'[foo]', 'bar', s),但它不会做任何事情.我究竟做错了什么?
我致力于为K Nearest Neighbors制作函数.我已经分别测试了每个功能,它们都运行良好.但是每当我把它们放在一起运行时KNN_method,它就会显示出来unhashable type: 'numpy.ndarray'.这是我的代码:
def distance(p,point):
import numpy as np
value = np.sqrt(sum(np.power((p-point),2)))
return(value)
def find_neighbors(p,list_of_points, k = 3):
import numpy as np
distances = np.zeros(list_of_points.shape[0])
for i in range(list_of_points.shape[0]):
distances[i]= distance(p,list_of_points[i])
ind = np.argsort(distances)
return(ind[0:k])
def majority_votes(votes):
import random
vote_result = {}
for key in votes:
if key in vote_result:
vote_result[key] += 1
else:
vote_result[key] = 1
final_list = []
for (number, vote) in vote_result.items():
if vote == max(vote_result.values()):
final_list.append(number)
Winner = random.choice(final_list)
return(Winner) …Run Code Online (Sandbox Code Playgroud) 为什么切片 4d 数组会给我一个 3d 数组?我期望在其中一个维度上有一个范围为 1 的 4d 数组。
例子:
print X.shape
(1783, 1, 96, 96)
Run Code Online (Sandbox Code Playgroud)
切片数组:
print X[11,:,:,:].shape
Run Code Online (Sandbox Code Playgroud)
或者
print X[11,:].shape
Run Code Online (Sandbox Code Playgroud)
给了我(1, 96, 96),但我期望(1, 1, 96, 96)
我可以通过print X[11:12,:].shape,但我想知道为什么第一种方法不能按我预期的那样工作?
我有以下问题,并没有想到没有for循环的方法.
考虑两个矩阵:
a=rand(N,3), b=rand(3,N)
我想得到的是:( b(:, i)*a(i, :)一个3*3矩阵)在所有的第三维中连接起来i.
因此,对于上述示例,结果应该是(3*3*N)矩阵.
我subprocess.run在循环中使用(超过 10 000 次)来调用一些 java 命令。像这样:
import subprocess
import tempfile
for i in range(10000):
ret = subprocess.run(["ls"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(_, name) = tempfile.mkstemp()
with open(name, 'w+') as fp:
fp.write(ret.stdout.decode())
Run Code Online (Sandbox Code Playgroud)
但是,一段时间后,我收到以下异常:
Traceback (most recent call last):
File "mwe.py", line 5, in <module>
ret = subprocess.run(["ls"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "/usr/lib/python3.5/subprocess.py", line 693, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1454, in _execute_child
errpipe_read, errpipe_write = os.pipe()
OSError: [Errno 24] …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Python中创建一个选项卡式GUI,我希望能够切换选项卡的启用/禁用状态(即阻止用户切换选项卡,并禁止鬼非活动选项卡以使这一事实显而易见).到目前为止,我一直无法弄清楚如何进行这种状态切换.
我决定使用Tkinter和/或Tix,因为它们内置于Windows上的Python发行版中(指导我的用户安装额外的第三方依赖项将比它的价值更麻烦).我曾经和Tkinter合作过一段时间但从未使用过Tix,因为标签似乎需要它.所以我在http://svn.python.org/projects/python/trunk/Demo/tix/samples/NoteBook.py上根据演示构建了一个双标签的Tix.NoteBook.
为了禁用选项卡,Tix选项卡实例的唯一相关属性(例如nb.hard_disk在演示代码中)似乎是configure()天真的做类似Tkinter的东西,即nb.hard_disk.configure(state=Tix.DISABLED)导致TclError: unknown option "-state"
搜索"禁用Tix笔记本选项卡"不会产生任何效果,甚至更通用的"禁用Tix小部件"也不会产生任何我能理解/使用的内容.感谢正确方向的任何指针.
我正在使用Boost.Interprocess在共享内存中创建一个映射(在本例中从字符串到字符串).在从地图检索期间,编译器似乎想强迫我在托管段中分配内存,以便(不必要地)包含查询项.
我希望能够通过将映射的键与已经在非共享内存中的实例进行匹配来更有效地在共享映射中查找值,而无需执行此额外分配.但是如果我尝试使用std::string或const char *作为地图find方法的参数,它就拒绝编译.(请参阅底部的编译器错误消息).
我是否需要在共享内存密钥类型和非共享内存密钥类型(std::string在此示例中)之间定义某种比较器方法?如果是这样,它应该是什么样的,我应该如何使地图使用它?如果没有,我该怎么办?
这是代码,后面是编译器错误.问题在于底层main().
// shmap2.cpp
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
//Typedefs of allocators and containers
namespace Shared
{
typedef boost::interprocess::managed_shared_memory
Segment;
typedef boost::interprocess::managed_shared_memory::segment_manager
SegmentManager;
typedef boost::interprocess::allocator< void, SegmentManager >
Allocator;
typedef boost::interprocess::allocator< char, SegmentManager >
CharAllocator;
typedef boost::interprocess::basic_string< char, std::char_traits< char >, CharAllocator >
String;
typedef std::less< String >
StringComparator;
// Definition of the shared map from String to String
// (To …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用nose_parameterized测试并希望将其用于单元测试方法。
from nose.tools import assert_equal
from nose_parameterized import parameterized
import unittest
Class TestFoo(unittest.TestCase):
def setUp(self):
self.user1 = "Bar"
self.user2 = "Foo"
@parameterized.expand([
("testuser1",self.user1,"Bar"),
("testuser2",self.user2,"Foo")
]
def test_param(self,name,input,expected):
assert_equal(input,expected)
Run Code Online (Sandbox Code Playgroud)
但self在装饰器函数中没有定义。有解决方法吗?我知道我可以使用全局类变量,但我需要在setUp.
python nose nosetests parameterized-unit-test nose-parameterized
以下代码是否安全且可移植?
void funcB(int *pA)
{
// Do something w/ pA
}
void funcA(int a)
{
funcB(&a);
// Do something else w/ a
}
Run Code Online (Sandbox Code Playgroud)
我的理解是,在许多系统上,函数参数被放置在 CPU 寄存器中而不是堆栈中。因此,您实际上是将寄存器的地址传递给函数,然后将其内容推送到堆栈中,以便可以将寄存器用于funcB. 因此,实际上,当您取消引用pAin 时funcB,您将获得地址pA而不是afrom的内容funcA。我的推理是正确的还是我完全偏离了基地?
因此,我尝试使用for循环以随机百分比更改一堆列表项.
import random as rdm
list = [1000, 100, 50, 25]
def change():
for item in list:
item = item + item*rdm.uniform(-10, 10)
change()
print(list)
Run Code Online (Sandbox Code Playgroud)
(我也不知道如何粘贴多行代码,所以我一个接一个地做了,也很感激帮助)现在当它打印列表时,它只包含它开始的数字.
python ×8
numpy ×2
audio ×1
audiosession ×1
boost ×1
c ×1
c++ ×1
deque ×1
list ×1
matlab ×1
matrix ×1
mixer ×1
nose ×1
nosetests ×1
process ×1
python-2.5 ×1
python-2.7 ×1
python-3.x ×1
regex ×1
subprocess ×1
tix ×1
tkinter ×1
windows ×1