线程库。
pythreadworker的Python项目详细描述
用python编写的线程库。帮助您构建线程应用程序。
此模块最初包含在ComicCrawler中。
功能
- 暂停、继续、停止并重新启动线程。
- 支持子线程。
- 轻松跨多个线程运行异步任务。
- 线程与事件之间的通信。
- 使用频道广播事件。
安装
pip install pythreadworker
用法示例
基本操作和事件:
#! python3# Always use worker.sleep. pyWorker would process event queue during# waiting.fromworkerimportcreate_worker,listen,sleep@create_workerdefincreaser():count=1@listen("SET_VALUE")def_(event):nonlocalcount# you don't need a lock to manipulate `count`count=event.datawhileTrue:print(count)# because the listener and the while loop are in the same threadcount+=1sleep(1)whileTrue:command=input("input command: ")ifcommand=="start":increaser.start()elifcommand=="stop":increaser.stop()elifcommand=="pause":increaser.pause()elifcommand=="resume":increaser.resume()elifcommand.startswith("set"):increaser.fire("SET_VALUE",int(command[4:]))elifcommand=="exit":increaser.stop()break
异步任务:
#! python3fromworkerimportaynsc_,sleepdeflong_work(t):sleep(t)return"Finished in {} second(s)".format(t)# The async task will be executed in another thread.pending=async_(long_work,5)# Do other stuff here...# Wait the thread to complete and get the result. If the task is already# finished, it returns directly with the result.print(pending.get())
使用频道广播事件:
#! python3fromworkerimportWorker,Channelchannel=Channel()defcreate_printer(name):printer=Worker()@printer.listen("PRINT")def_(event):print(name,"recieved",event.data)channel.sub(printer)returnprinter.start()foo=create_printer("foo")bar=create_printer("bar")channel.pub("PRINT","Hello channel!")foo.stop()bar.stop()
子线程和事件冒泡/广播:
#! python3fromworkerimportWorker,sleepdefcreate_thread(name,parent):thread=Worker(parent=parent)@thread.listen("HELLO")def_(event):print(name)returnthread.start()parent=create_thread("parent",None)child=create_thread("child",parent)grand=create_thread("grand",child)# broadcast/bubble is happened in main thread. It doesn't gaurantee the# execution order of listeners.parent.fire("HELLO",broadcast=True)sleep(1)grand.fire("HELLO",bubble=True)sleep(1)# stop a parent thread would also stop its childrenparent.stop()
工作原理
模块为每个线程(包括主线程)创建一个事件队列。当调用阻塞函数(worker.sleep、worker.wait_event、worker.Async.get等)时,它们进入事件循环,以便线程可以处理事件、与其他线程通信或在调用期间引发异常。
这也意味着,如果您不使用PythRead工作人员提供的函数,则该模块没有影响您现有代码的机会。与其他框架一起工作应该很容易。
更改日志
- 0.9.0(2018年6月8日)
- change:“later()”的签名已更改。您应该使用它在特定线程上计划任务。
- change:一旦线程停止,由“listener()”快捷方式注册的侦听器将被删除。
- 将permanent和once参数添加到Listener。
- 添加:Defer。处理跨线程通信的实用程序。
- 0.8.0(2017年3月26日)
- 将“打印追踪”选项添加到工作进程。
- 能够使用later作为装饰器。
- 把所有的都放进初始py中。
- 函数重命名:异步->;异步,同步->;等待。
- async现在扩展了worker并需要start()来运行。
- later()现在默认情况下不使用当前线程作为目标。要使用当前线程作为目标,请传递target=true。
- 各种函数都可以用作decorator,包括await_, async_, later。
- 删除守护进程线程,使用守护进程工作进程。
- 添加Worker.wait_until。
- 添加create_worker。
- 重构。
- 0.7.0(2017年2月26日)
- 改进文档。
- 删除def target(thread)语法,改用current()获取当前线程。
- 使用派林和狮身人面像。
- 导出more shortcuts。
- 0.6.0(2016年7月1日)
- 添加thread.later。
- 0.5.1(2016年4月22日)
- 使用float in sleep函数。
- 0.5.0(2016年4月22日)
- 添加同步。
- 0.4.0(2016年4月20日)突破性变化
- 接口完全更改
- 放置Message.put, .get
- 放置UserWorker
- 删除Worker.create_child。在构造函数中改用parent选项。
- 放置global_cleanup
- 添加sleep
- 添加current
- 添加Channel
- 添加Listener.priority
- 将daemon选项添加到Worker
- Worker.cleanup->;Worker.update
- Worker.message->;Worker.fire
- Worker.wait_message->;Worker.wait_event
- Worker.message_loop->;Worker.wait_forever
- 0.3.0(2015年6月14日)
- 捕获BaseException。