Thread safe message queue
Project description
threadmsg
Thread safe message queue with function calls
Table of contents
Install
$ pip3 install threadmsg
Examples
import threadmsg as tm
#--------------------------------------------------------------------
# Example 1
async def msgThread(ctx):
# Check for message
msg = ctx.getMsg()
if not msg:
return
print(msg)
# Return a negative number to exit thread
return -1
# Return a positive number and this function will be
# called again after that number of seconds
return 3.5
# Return zero this function will be called again immediately
return 0
# Return nothing for inifinite wait, this function will only
# be called again if a message is posted or quit is requested
return
# Create a thread
t1 = tm.ThreadMsg(msgThread)
# Send message to thread
t1.addMsg("Hello thread")
# Wait for thread to exit
t1.join()
#--------------------------------------------------------------------
# Example 2
async def myThread(ctx, v1, v2):
# Verify parameters
assert 5 == v1
assert 6 == v2
# Check if this is the first run
if not ctx.loops:
print('First call')
return 0
# Check if thread is exiting
if not ctx.run:
print('Thread is exiting')
return
print('Second call')
# Exit thread
return -1
# Create a thread, but don't start it
t1 = tm.ThreadMsg(myThread, (5, 6), False)
# Start thread
t1.start()
# Wait for thread to exit
t1.join()
#--------------------------------------------------------------------
# Example 3
class funThread(tm.ThreadMsg):
def __init__(self, start=True):
super().__init__(self.msgThread, deffk='_funName', start=start)
self.callMap = {
'add': self.add
}
@staticmethod
async def msgThread(ctx):
while msg := ctx.getMsg():
await ctx.mapMsgAsync(None, ctx.callMap, msg)
# return nothing so this function is only
# called again when there is a message
def add(self, a, b):
return a + b
# Create the thread
t1 = funThread()
def showReturn(ctx, params, retval, err):
print(params, retval, err)
# Call add function with callback
t1.call(showReturn, 'add', a=1, b=2)
# Wait for reply from add function
reply = t1.call('add', a=1, b=2)
await reply.wait(3)
print(reply.getData())
# Signal thread to quit
t1.stop()
# Wait for thread to exit
t1.join()
# Or call join with True to both signal quit and wait for exit
# tw.join(True)
References
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file threadmsg-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: threadmsg-0.2.4-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95caf060c1dbe7dbbb06d1fbb081d3f70cf269849df1c53d98e0ea4417079640 |
|
MD5 | 1881001cc5bcc8bec02fd706bce3702f |
|
BLAKE2b-256 | 87e8cf3a52f62a76eb22efbd95b052013ec492224fdd93f9235940ecca0561c8 |