线程从另一个线程中断(Thread interruption from another thread)
我使用这样的东西创建了9个线程(所有线程都会处理无限循环)
void printStr(); thread func_thread(printStr); void printStr() { while (true) { cout << "1\n"; this_thread::sleep_for(chrono::seconds(1)); } }
我还创建了第10个线程来控制它们。 我将如何阻止或杀死我的第10条线中的任何一条? 或者请提出另一种机制。
I'm creating 9 threads using something like this (all threads will process infinity loop)
void printStr(); thread func_thread(printStr); void printStr() { while (true) { cout << "1\n"; this_thread::sleep_for(chrono::seconds(1)); } }
I also create 10th thread to control them. How would I stop or kill any of this 9 threads from my 10th? Or suggest another mechanism please.
原文:https://stackoverflow.com/questions/23188275
最满意答案
这里的行是说将
Sender
列作为源节点,将Recipient
列作为Target,并将time
添加为edge属性 。 因此,您只在Sender和Recipient之间创建单个(定向)边,并且只将最后一行的时间添加为边的属性。email = nx.from_pandas_dataframe(email_df, '#Sender', 'Recipient', edge_attr = 'time')
您只能为一对节点定义一条边 - 您可以在构建网络之前对数据框进行分组,并将计数用作边的权重,
edge_groups = email_df.groupby(["#Sender", "Recipient"], as_index=False).count().rename(columns={"time":"weight"}) email = nx.from_pandas_dataframe(edge_groups, '#Sender', 'Recipient', edge_attr = 'weight')
Your line here is saying to take the
Sender
column as the source node, theRecipient
column as the Target and add thetime
as edge attributes. So you are only creating a single (directed) edge between Sender and Recipient, and only the time of the last row will be added as an attribute of the edge.email = nx.from_pandas_dataframe(email_df, '#Sender', 'Recipient', edge_attr = 'time')
You can only have one edge defined for a pair of nodes - you could group the dataframe before constructing your network and use the count as the weights for the edges,
edge_groups = email_df.groupby(["#Sender", "Recipient"], as_index=False).count().rename(columns={"time":"weight"}) email = nx.from_pandas_dataframe(edge_groups, '#Sender', 'Recipient', edge_attr = 'weight')
相关问答
更多-
假设数据框架: df = pd.DataFrame({'a':['5','10'], 'b':['9','7']}) 目前的数据类型: In [391]: df.dtypes Out[391]: a object b object 将整个df列转换为数字: df = df.apply(pd.to_numeric) 结果: In [393]: df.dtypes Out[393]: a int64 b int64 仅将选择列转换为数字: In [396]: df = pd. ...
-
将pandas dataframe中的对象列转换为datetime(Converting object column in pandas dataframe to datetime)[2023-05-03]
这意味着你有一个额外的空间。 虽然pd.to_datetime非常擅长解析日期而通常没有指定任何格式,但是当您实际指定格式时,它必须完全匹配。 您可以通过添加.str.strip()来解决您的问题,以在转换之前删除额外的空格。 import pandas as pd df['Time stamp'] = pd.to_datetime(df['Time stamp'].str.strip(), format='%d/%m/%Y') 或者,您可以使用dayfirst=True参数来利用其解析各种格式的日期的功 ... -
如果您将边和节点都作为列表,那么在networkx中构建图表非常简单。 鉴于您在构建图形对象时出现问题,最好的诊断方法是逐步完成networkx中的图形构建: import networkx as NX import string import random G = NX.Graph() # initialize the graph # just generate some synthetic data for the nodes and edges: my_nodes = [ ch for ch ...
-
将pandas数据帧转换为Networkx对象时,边数不同(Number of edges differ when converting pandas dataframe to Networkx object)[2022-06-04]
这里的行是说将Sender列作为源节点,将Recipient列作为Target,并将time添加为edge属性 。 因此,您只在Sender和Recipient之间创建单个(定向)边,并且只将最后一行的时间添加为边的属性。 email = nx.from_pandas_dataframe(email_df, '#Sender', 'Recipient', edge_attr = 'time') 您只能为一对节点定义一条边 - 您可以在构建网络之前对数据框进行分组,并将计数用作边的权重, edge_grou ... -
本教程基于networkx的先前版本,其中g.edges()或g.edges(Data=True)会给你一个元组列表。 列表是可订阅的。 您正在运行的版本具有不同的输出, g.edges为您提供了一个EdgeView属性,而g.edges(data=True)是一个不可下标的EdgeDataView对象。 要回答你的问题,你可以这样做: list(g.edges(data=True))[0:5] 注意:对于g.nodes()也是如此:在它现在是一个list之前,它是一个NodeView属性而不是可下载的。 ...
-
您可以使用g.loc选择isconfirm为0的所有行: In [90]: g.loc[:, 0] Out[90]: ID 0 0.827957 1 0.911111 2 0.944954 3 0.884956 4 0.931373 5 0.869048 6 0.941176 7 0.884615 8 0.901961 9 0.930693 Name: isconfirm, dtype: float64 [:, 0]表示索引的第二级中的值。 因 ...
-
在数据帧上使用replace方法: import numpy as np df = DataFrame({ 'k1': ['na'] * 3 + ['two'] * 4, 'k2': [1, 'na', 2, 'na', 3, 4, 4]}) print df df = df.replace('na', np.nan) print df 我认为指出df.replace('na',np.nan)本身不起作用是有帮助的。 您必须将其分配回现有数据框。 Use the replace method on ...
-
原因是图表是无向的。 igraph和networkx处理I - J平局和J - I networkx对待。 panda.intersection只会处理完全匹配(即数据帧A中的第1列与数据帧B中的第1列匹配,数据帧A中的第2列与数据帧B中的第3列匹配)。 library(igraph); library(dplyr) set.seed(1034) g1 <- sample_gnp(20, 0.25, directed = F) set.seed(1646) g2 <- sample_gnp(20, 0.25 ...
-
一个更小的问题可能有助于获得答案。 (例如,你的x.py没有任何内容与x.py的问题相关) 为了帮助您入门,您可能会遇到一些名称空间重载(重命名相同的对象)。 在netxgui.py您import networkx as nx但稍后您会声明一个类nx(QtGui.QDialog) 。 在此之后你尝试调用nx.spring_layout() ,你可能想要从networkx而不是你的自定义nx类。 您尝试访问的结构可能存在于networkx Graph实例中但不存在于QDialog实例中? 通常,避免使用fro ...
-
虽然您可以使用nx.non_edges()找到图中缺少边的位置,但该函数实际上并不返回可以分配数据的对象 - 毕竟逻辑图中不存在非边。 但是,您可以创建第二个图形,其中包含不在第一个图形中的所有边缘,然后将分数分配给第二个图形中的边缘。 import networkx as nx import numpy as np G1 = nx.MultiGraph() G1.add_edges_from([ ('1', '2'), ('1', '3'), ('1', '5'), ( ...