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

网站模板下载百度云链接怎么做的房管局在线咨询

网站模板下载百度云链接怎么做的,房管局在线咨询,wordpress win8,做软装找产品上哪个网站python-从线程返回值我如何获得一个线程以将元组或我选择的任何值返回给Python中的父级#xff1f;12个解决方案59 votes我建议您在启动线程之前实例化Queue.Queue#xff0c;并将其作为线程的args之一传递#xff1a;在线程完成之前#xff0c;它.puts将其结果作为参数接收…python-从线程返回值我如何获得一个线程以将元组或我选择的任何值返回给Python中的父级12个解决方案59 votes我建议您在启动线程之前实例化Queue.Queue并将其作为线程的args之一传递在线程完成之前它.puts将其结果作为参数接收到队列中。 家长可以随意将其设置为.get或.get_nowait。队列通常是在Python中安排线程同步和通信的最佳方法队列本质上是线程安全的消息传递工具这是组织多任务的最佳方法Alex Martelli answered 2019-11-10T12:55:40Z12 votes如果要调用join()等待线程完成则只需将结果附加到Thread实例本身然后在join()返回之后从主线程检索它。另一方面您没有告诉我们您打算如何发现线程已完成并且结果可用。 如果您已经有这样做的方法它可能会为您(和我们如果您要告诉我们)指出获得结果的最佳方法。Peter Hansen answered 2019-11-10T12:56:14Z12 votes您应该将Queue实例作为参数传递然后将返回对象.put()放入队列。 您可以通过queue.get()收集放置的任何对象的返回值。样品queue Queue.Queue()thread_ threading.Thread(targettarget_method,nameThread1,args[params, queue],)thread_.start()thread_.join()queue.get()def target_method(self, params, queue):Some operations right hereyour_return Whatever your object isqueue.put(your_return)用于多线程#Start all threads in thread poolfor thread in pool:thread.start()response queue.get()thread_results.append(response)#Kill all threadsfor thread in pool:thread.join()我使用此实现对我来说非常有用。 我希望你这样做。Fatih Karatana answered 2019-11-10T12:56:55Z7 votes使用lambda包装目标线程函数然后使用队列将其返回值传递回父线程。 (您的原始目标函数将保持不变而无需额外的队列参数。)示例代码import threadingimport queuedef dosomething(param):return param * 2que queue.Queue()thr threading.Thread(target lambda q, arg : q.put(dosomething(arg)), args (que, 2))thr.start()thr.join()while not que.empty():print(que.get())输出4Petr Vepřek answered 2019-11-10T12:57:27Z7 votes我很惊讶没有人提到您可以将其传递给可变对象 thread_return{success: False} from threading import Thread def task(thread_return):... thread_return[success] True... Thread(targettask, args(thread_return,)).start() thread_return{success: True}也许这是我不知道的主要问题。jcomeau_ictx answered 2019-11-10T12:57:59Z5 votes另一种方法是将回调函数传递给线程。 这提供了一种简单安全和灵活的方式可以随时从新线程中将值返回给父级。# A sample implementationimport threadingimport timeclass MyThread(threading.Thread):def __init__(self, cb):threading.Thread.__init__(self)self.callback cbdef run(self):for i in range(10):self.callback(i)time.sleep(1)# testimport sysdef count(x):print xsys.stdout.flush()t MyThread(count)t.start()Vijay Mathew answered 2019-11-10T12:58:23Z3 votes您可以使用同步队列模块。考虑您需要从具有已知ID的数据库中检查用户信息def check_infos(user_id, queue):result send_data(user_id)queue.put(result)现在您可以像这样获取数据import queue, threadingqueued_request queue.Queue()check_infos_thread threading.Thread(targetcheck_infos, args(user_id, queued_request))check_infos_thread.start()final_result queued_request.get()BoCyrill answered 2019-11-10T12:59:01Z2 votesPOCimport randomimport threadingclass myThread( threading.Thread ):def __init__( self, arr ):threading.Thread.__init__( self )self.arr arrself.ret Nonedef run( self ):self.myJob( self.arr )def join( self ):threading.Thread.join( self )return self.retdef myJob( self, arr ):self.ret sorted( self.arr )return#Call the main method if run from the command line.if __name__ __main__:N 100arr [ random.randint( 0, 100 ) for x in range( N ) ]th myThread( arr )th.start( )sortedArr th.join( )print arr2: , sortedArrhusanu answered 2019-11-10T12:59:25Z1 votes好吧在Python线程模块中有一些与锁关联的条件对象。 一种方法acquire()将返回从基础方法返回的任何值。 有关更多信息Python条件对象Ben Hayden answered 2019-11-10T12:59:51Z1 votes基于jcomeau_ictx的建议。 我遇到的最简单的一个。 此处的要求是从服务器上运行的三个不同进程获取退出状态状态并在三个进程均成功时触发另一个脚本。 这似乎工作正常class myThread(threading.Thread):def __init__(self,threadID,pipePath,resDict):threading.Thread.__init__(self)self.threadIDthreadIDself.pipePathpipePathself.resDictresDictdef run(self):print Starting thread %s % (self.threadID)if not os.path.exists(self.pipePath):os.mkfifo(self.pipePath)pipe_fd os.open(self.pipePath, os.O_RDWR | os.O_NONBLOCK )with os.fdopen(pipe_fd) as pipe:while True:try:message pipe.read()if message:print Received: %s % messageself.resDict[success]messagebreakexcept:passtResSer{success:0}tResWeb{success:0}tResUisvc{success:0}threads []pipePathSer/tmp/path1pipePathWeb/tmp/path2pipePathUisvc/tmp/path3th1myThread(1,pipePathSer,tResSer)th2myThread(2,pipePathWeb,tResWeb)th3myThread(3,pipePathUisvc,tResUisvc)th1.start()th2.start()th3.start()threads.append(th1)threads.append(th2)threads.append(th3)for t in threads:print t.join()print Res: tResSer %s tResWeb %s tResUisvc %s % (tResSer,tResWeb,tResUisvc)# The above statement prints updated values which can then be further processedf-z-N answered 2019-11-10T13:00:17Z0 votes以下包装函数将包装现有函数并返回一个对象该对象既指向线程(因此您可以在其上调用threading.Thread、join()等)也可以访问/查看其最终返回值。def threadwrap(func,args,kwargs):class res(object): resultNonedef inner(*args,**kwargs):res.resultfunc(*args,**kwargs)import threadingt threading.Thread(targetinner,argsargs,kwargskwargs)res.threadtreturn resdef myFun(v,debugFalse):import timeif debug: print Debug mode ONtime.sleep(5)return v*2xthreadwrap(myFun,[11],{debug:True})x.thread.start()x.thread.join()print x.result看起来还可以并且threading.Thread类似乎可以通过这种功能轻松扩展(*)所以我想知道为什么它还不存在。 上述方法是否有缺陷(*)请注意husanu对这个问题的回答恰好做到了将threading.Thread子类化得到join()给出返回值的版本。Andz answered 2019-11-10T13:01:01Z0 votes对于简单的程序以上答案对我来说似乎有点过头了。 我会采纳这种可变方法class RetVal:def __init__(self):self.result Nonedef threadfunc(retVal):retVal.result your return valueretVal RetVal()thread Thread(target threadfunc, args (retVal))thread.start()thread.join()print(retVal.result)TheTrowser answered 2019-11-10T13:01:26Z
http://www.yutouwan.com/news/275795/

相关文章:

  • 免费自己制作网站教程wordpress修改标题
  • wordpress默认站点网站建设方案ppt下载
  • 做网站快速赚钱wordpress自定义注册邮件
  • 网络建站新品发布会英语
  • 用什么工具做网站视图长沙有什么好玩的地方
  • 微信与网站对接软件工程师的就业前景
  • 网站建设公司做销售好不好网站关键词快速排名优化
  • 网站专题教程php网站后台开发教程
  • 如何做英文网站外链展示型网站企业网站建设
  • 帮人做网站赚钱wordpress分类目录高亮
  • 做一个公司网站大概要多少钱线上seo关键词优化软件工具
  • 湖南省建设厅官方网站百度广告搜索引擎
  • 好点子网站建设太原网站建设杰迅
  • 做英文网站要做适合已经的咨询公司网站建设
  • 网站报错404wordpress不能发文章_只能在标题内写字
  • 阿里云服务器怎么放网站网站开发分前台后台
  • 河北邢台做网站杭州室内设计工作室
  • 云服务器建设网站教程眉山市做网站的公司
  • 怎么选择佛山网站设计建筑设计专业世界大学排名
  • 南昌专业网站排名推广在线动画手机网站模板
  • 阿里云网站建设的功能外贸产品推广网站
  • 天津市建设工程交易中心网站免费做金融网站
  • 东莞网站建设制作免费咨免费推广网址
  • 宁波网站推广渠道ps做好切片后怎么做网站
  • 成都网页设计的网站建设网站地图在线生成
  • 优秀网站开发公司成都专业制作网页的公司
  • 苏州专业网站建设开发公司建设网站费用明细
  • 关于电视剧的网站设计网页网站推广该怎么做
  • 旅游网站建设的组织性南康市建设局网站
  • 长沙专业建设网站局域网网站架设软件