{"id":191,"date":"2016-05-26T16:44:36","date_gmt":"2016-05-26T16:44:36","guid":{"rendered":""},"modified":"2016-05-26T16:44:36","modified_gmt":"2016-05-26T16:44:36","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=191","title":{"rendered":"Python callback demo"},"content":{"rendered":"<p><span style=\"font-size:14px;\">import threading<\/span><br \/>\n<span style=\"font-size:14px;\">import time<\/span><\/p>\n<p><span style=\"font-size:14px;\">class TestThread(threading.Thread):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; def __init__(self,id,callback):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; threading.Thread.__init__(self)<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; self.id = id<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; self.callback = callback<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; self.active = True<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; &nbsp; def run(self):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; while self.active:<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.callback(self.id)<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(1)<\/span><\/p>\n<p>\n<span style=\"font-size:14px;\">class sessions:<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; def __init__(self):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; self.thread = None<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; self.str=&#8221;callback id:&#8221;<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; &nbsp; def callback(self, id):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; print self.str,id<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; &nbsp; def create_thread(self, id):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; self.thread=TestThread(id,self.callback)<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; self.thread.start()<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; &nbsp; def clean_thread(self):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; if self.thread:<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.thread.active=False<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.thread=None<\/span><\/p>\n<p>\n<span style=\"font-size:14px;\">if __name__ == &#8216;__main__&#8217;:<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; sess=sessions()<\/span><\/p>\n<p><span style=\"font-size:14px;\">&nbsp; &nbsp; for i in range(100):<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; sess.create_thread(i)<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(1)<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; sess.clean_thread()<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p><span style=\"font-size:14px;\">import threading<\/span><br \/><span style=\"font-size:14px;\">import time<\/span><\/p>\n<p><span style=\"font-size:14px;\">class TestThread(threading.Thread):<\/span><br \/><span style=\"font-size:14px;\">&nbsp; &nbsp; def __init__(self,id,callback):<\/span><br \/><span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; threading.Thread.__init__(self)<\/span><br \/><span style=\"font-size:14px;\">&nbsp; &nbsp; &nbsp; &nbsp; self.i&#8230;<\/span><\/p>\n","protected":false},"author":1,"featured_media":560,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[334],"tags":[],"class_list":["post-191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python callback demo - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=191\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python callback demo - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"import threadingimport timeclass TestThread(threading.Thread):&nbsp; &nbsp; def __init__(self,id,callback):&nbsp; &nbsp; &nbsp; &nbsp; threading.Thread.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.i...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=191\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-26T16:44:36+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/weizn.net\/?p=191#primaryimage\",\"inLanguage\":\"zh-Hans\",\"url\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/w6n3meqs5ksiak2pnn0qw1kessd2-ua3fq3x40.jpg\",\"contentUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/w6n3meqs5ksiak2pnn0qw1kessd2-ua3fq3x40.jpg\",\"width\":1280,\"height\":720},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=191#webpage\",\"url\":\"http:\/\/weizn.net\/?p=191\",\"name\":\"Python callback demo - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=191#primaryimage\"},\"datePublished\":\"2016-05-26T16:44:36+00:00\",\"dateModified\":\"2016-05-26T16:44:36+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=191#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=191\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=191#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python callback demo\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=191#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=191#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"Python callback demo\",\"datePublished\":\"2016-05-26T16:44:36+00:00\",\"dateModified\":\"2016-05-26T16:44:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=191#webpage\"},\"wordCount\":205,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"image\":{\"@id\":\"http:\/\/weizn.net\/?p=191#primaryimage\"},\"thumbnailUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/w6n3meqs5ksiak2pnn0qw1kessd2-ua3fq3x40.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=191#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python callback demo - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=191","og_locale":"zh_CN","og_type":"article","og_title":"Python callback demo - Wayne&#039;s Blog","og_description":"import threadingimport timeclass TestThread(threading.Thread):&nbsp; &nbsp; def __init__(self,id,callback):&nbsp; &nbsp; &nbsp; &nbsp; threading.Thread.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.i...","og_url":"http:\/\/weizn.net\/?p=191","og_site_name":"Wayne&#039;s Blog","article_published_time":"2016-05-26T16:44:36+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"ImageObject","@id":"http:\/\/weizn.net\/?p=191#primaryimage","inLanguage":"zh-Hans","url":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/w6n3meqs5ksiak2pnn0qw1kessd2-ua3fq3x40.jpg","contentUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/w6n3meqs5ksiak2pnn0qw1kessd2-ua3fq3x40.jpg","width":1280,"height":720},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=191#webpage","url":"http:\/\/weizn.net\/?p=191","name":"Python callback demo - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/weizn.net\/?p=191#primaryimage"},"datePublished":"2016-05-26T16:44:36+00:00","dateModified":"2016-05-26T16:44:36+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=191#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=191"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=191#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"Python callback demo"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=191#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=191#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"Python callback demo","datePublished":"2016-05-26T16:44:36+00:00","dateModified":"2016-05-26T16:44:36+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=191#webpage"},"wordCount":205,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"image":{"@id":"http:\/\/weizn.net\/?p=191#primaryimage"},"thumbnailUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/w6n3meqs5ksiak2pnn0qw1kessd2-ua3fq3x40.jpg","articleSection":["Python"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=191#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/media\/560"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}