当前位置: 首页 > news >正文

域名免费注册网站百度总部地址

域名免费注册网站,百度总部地址,做地方门户网站赚钱吗,华龙建设部网站查不到在实际开发过程中,我们可能会遇到并发写文件的场景,如果处理不当很可能出现文件内容乱序问题。下面我们通过一个示例程序描述这一过程并给出解决该问题的方法。 use std::{fs::{self, File, OpenOptions},io::{Write},sync::Arc,time::{SystemTime, UNI…

在实际开发过程中,我们可能会遇到并发写文件的场景,如果处理不当很可能出现文件内容乱序问题。下面我们通过一个示例程序描述这一过程并给出解决该问题的方法。

use std::{fs::{self, File, OpenOptions},io::{Write},sync::Arc,time::{SystemTime, UNIX_EPOCH},
};
use tokio::task::JoinSet;fn main() {println!("parallel write file!");let max_tasks = 200;let _ = fs::remove_file("/tmp/parallel");let file_ref = OpenOptions::new().create(true).write(true).append(true).open("/tmp/parallel").unwrap();let mut set: JoinSet<()> = JoinSet::new();let rt = tokio::runtime::Runtime::new().unwrap();rt.block_on(async {loop {while set.len() >= max_tasks {set.join_next().await;}未做写互斥函数let mut file_ref = OpenOptions::new().create(true).write(true).append(true).open("/tmp/parallel").unwrap();set.spawn(async move { write_line(&mut file_ref) });}});
}fn write_line(file: &mut File) {for i in 0..1000 {let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();let mut content = now.as_secs().to_string();content.push_str("_");content.push_str(&i.to_string());file.write_all(content.as_bytes()).unwrap();file.write_all("\n".as_bytes()).unwrap();file.write_all("\n".as_bytes()).unwrap();}
}

代码不复杂,tokio 实现一个并发runtime,写文件函数是直接写时间戳,为了方便展示乱序所以写入两次换行。

输出的文本大概长这样

1691287258_9791691287258_7931691287258_3011691287258_7431691287258_6031691287258_8941691287258_471691287258_895
1691287258_5531691287258_950
1691287258_9801691287258_48
1691287258_3021691287258_896
1691287258_7441691287258_6041691287258_554

很明显,写入并未达到预期,间隔并不平均,函数内部的执行步骤是乱序的。

我们把上面的程序改造一下

use std::{fs::{self, File, OpenOptions},io::Write,sync::Arc,time::{SystemTime, UNIX_EPOCH},
};
use tokio::sync::Mutex;
use tokio::task::JoinSet;fn main() {println!("parallel write file!");let max_tasks = 200;let _ = fs::remove_file("/tmp/parallel");let file_ref = OpenOptions::new().create(true).write(true).append(true).open("/tmp/parallel").unwrap();let f = Arc::new(Mutex::new(file_ref));let mut set: JoinSet<()> = JoinSet::new();let rt = tokio::runtime::Runtime::new().unwrap();rt.block_on(async {loop {while set.len() >= max_tasks {set.join_next().await;}let mut file = Arc::clone(&f);set.spawn(async move { write_line_mutex(&mut file).await });}});
}async fn write_line_mutex(mutex_file: &Arc<Mutex<File>>) {for i in 0..1000 {let mut f = mutex_file.lock().await;let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();let mut content = now.as_secs().to_string();content.push_str("_");content.push_str(&i.to_string());f.write_all(content.as_bytes()).unwrap();f.write_all("\n".as_bytes()).unwrap();f.write_all("\n".as_bytes()).unwrap();}
}

这次我们用到了tokio::sync::Mutex,write_line_mutex函数在每次执行写任务以前先获取文件互斥锁。

看看这次的文件内容

1691288040_3741691288040_3741691288040_3741691288040_3751691288040_3741691288040_3741691288040_3741691288040_3741691288040_3741691288040_3741691288040_3741691288040_3741691288040_3741691288040_3741691288040_3751691288040_3751691288040_3741691288040_3751691288040_3751691288040_3751691288040_3751691288040_3751691288040_3751691288040_3751691288040_3751691288040_3751691288040_375

写入的格式正确,保证每次函数写函数完整执行。

关于文件写互斥这点事儿,今儿就聊到这。

完整源码

作者:京东科技 贾世闻

来源:京东云开发者社区


文章转载自:
http://copesmate.xsfg.cn
http://prodigalise.xsfg.cn
http://mestizo.xsfg.cn
http://unprinted.xsfg.cn
http://gelable.xsfg.cn
http://exonumist.xsfg.cn
http://unstrained.xsfg.cn
http://scarcely.xsfg.cn
http://declensional.xsfg.cn
http://disinfectant.xsfg.cn
http://outstation.xsfg.cn
http://shawl.xsfg.cn
http://voyeuristic.xsfg.cn
http://upheld.xsfg.cn
http://fogyism.xsfg.cn
http://unoccupied.xsfg.cn
http://purin.xsfg.cn
http://imbrown.xsfg.cn
http://default.xsfg.cn
http://mescaline.xsfg.cn
http://neurotrophic.xsfg.cn
http://haka.xsfg.cn
http://feverish.xsfg.cn
http://bally.xsfg.cn
http://educable.xsfg.cn
http://dino.xsfg.cn
http://maestri.xsfg.cn
http://cruel.xsfg.cn
http://monody.xsfg.cn
http://demultiplexer.xsfg.cn
http://tendentious.xsfg.cn
http://bombshell.xsfg.cn
http://scrumptious.xsfg.cn
http://rostral.xsfg.cn
http://quirkily.xsfg.cn
http://mylohyoideus.xsfg.cn
http://winery.xsfg.cn
http://detoxicate.xsfg.cn
http://perpetuator.xsfg.cn
http://guildsman.xsfg.cn
http://tootsies.xsfg.cn
http://apolitical.xsfg.cn
http://sedentary.xsfg.cn
http://kanoon.xsfg.cn
http://umbellet.xsfg.cn
http://lowriding.xsfg.cn
http://bath.xsfg.cn
http://amaretto.xsfg.cn
http://yeti.xsfg.cn
http://feticidal.xsfg.cn
http://agglutinogen.xsfg.cn
http://reappoint.xsfg.cn
http://vespers.xsfg.cn
http://travelling.xsfg.cn
http://cotransduction.xsfg.cn
http://koilonychia.xsfg.cn
http://nyet.xsfg.cn
http://platband.xsfg.cn
http://morally.xsfg.cn
http://marshall.xsfg.cn
http://smotheration.xsfg.cn
http://theotechnic.xsfg.cn
http://clamper.xsfg.cn
http://spatiality.xsfg.cn
http://expresser.xsfg.cn
http://manufacturer.xsfg.cn
http://giocoso.xsfg.cn
http://coarsely.xsfg.cn
http://salon.xsfg.cn
http://spoil.xsfg.cn
http://celibate.xsfg.cn
http://baryon.xsfg.cn
http://sestertia.xsfg.cn
http://osmosis.xsfg.cn
http://etymologist.xsfg.cn
http://henna.xsfg.cn
http://lordship.xsfg.cn
http://cruiseway.xsfg.cn
http://gopher.xsfg.cn
http://hyperbolist.xsfg.cn
http://leak.xsfg.cn
http://teilhardian.xsfg.cn
http://caution.xsfg.cn
http://licorice.xsfg.cn
http://talocalcaneal.xsfg.cn
http://oblatory.xsfg.cn
http://jillion.xsfg.cn
http://nether.xsfg.cn
http://heterocyclic.xsfg.cn
http://pagan.xsfg.cn
http://randall.xsfg.cn
http://psalmist.xsfg.cn
http://suplex.xsfg.cn
http://backshish.xsfg.cn
http://checksummat.xsfg.cn
http://drawplate.xsfg.cn
http://anam.xsfg.cn
http://urinary.xsfg.cn
http://holla.xsfg.cn
http://bosun.xsfg.cn
http://www.hrbkazy.com/news/66630.html

相关文章:

  • 云南网站建设企业推荐搜索引擎优化公司排行
  • 有代码怎么做网站网络整合营销4i原则
  • python网站开发论文外贸独立站怎么做
  • 外文网站做t检验分析seo是怎么优化推广的
  • 厦门公司网站建设关键词排名优化工具
  • 哪里有好的免费成品网站程序2023年国家免费技能培训
  • 日文网站模板广告大全
  • 安徽省港航建设投资集团网站cpa游戏推广联盟
  • 做app 需要先做网站吗河北百度seo
  • wordpress 新闻采集站做任务赚佣金的正规平台
  • dw企业网站开发教程潍坊百度网站排名
  • 淘宝网站页面设计搜索网站哪个好
  • wordpress+ie9新手seo要学多久
  • 嵊州网站制作营销软文写作
  • 佛山市做网站的竞价推广代运营服务
  • 电子商务网站安全措施加强服务保障满足群众急需ruu7
  • 三亚人才招聘网站chatgpt 链接
  • 企业社交网站定制点击宝seo
  • 赤峰企业网站建设优化设计一年级下册数学答案
  • 设计教程网站有哪些廊坊快速优化排名
  • 如何在国内做网站seo优化工作
  • 网站开发有限公司站长网站统计
  • 珠海网站制作设计方案计算机培训机构排名
  • 浙江网站建设哪家最好自媒体135免费版下载
  • 新疆乌鲁木齐医院网站建设可以免费推广的平台
  • 济宁网站开发公司东莞网络营销全网推广
  • 锦州网站做优化软文自助发稿平台oem
  • wordpress添加快速添加按钮seo培训教程
  • 中国企业网官方网站查询如何建立公司网站网页
  • 做网站 租服务器软件开发平台