-
Notifications
You must be signed in to change notification settings - Fork 402
Create multi-threading.py (example) #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # -*- coding: UTF-8 -*- | ||
|
|
||
| from fbchat import Client | ||
| from fbchat.models import * | ||
| import threading | ||
|
|
||
| # Subclass fbchat.Client and override required methods | ||
| class PrintMessage(Client): | ||
| def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs): | ||
| self.markAsDelivered(thread_id, message_object.uid) | ||
| self.markAsRead(thread_id) | ||
|
|
||
| print("\nIncoming message: {}".format(message_object.text)) | ||
|
|
||
| # Creating some feedback on login since we will disable INFO logging | ||
| def onLoggedIn(self, email=None): | ||
| print("Login of {} successful.".format(email)) | ||
|
|
||
|
|
||
| # Logging in and setting logging level to WARNING to avoid some unessential output | ||
| client = PrintMessage("<email>", "<password>", logging_level=30) | ||
|
joakimed marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def send(): | ||
|
joakimed marked this conversation as resolved.
Outdated
|
||
| while True: | ||
| payload = input("Message: ") | ||
| if payload: | ||
| client.send( | ||
|
joakimed marked this conversation as resolved.
Outdated
|
||
| Message(text=payload), thread_id=client.uid, thread_type=ThreadType.USER | ||
| ) | ||
|
|
||
|
|
||
| def receive(): | ||
| while True: | ||
| client.doOneListen() | ||
|
joakimed marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| # Creating and starting separate threads for handling the receiving and sending of messages | ||
| t1 = threading.Thread(target=receive) | ||
| t2 = threading.Thread(target=send) | ||
| t1.start() | ||
| t2.start() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed one thread, and made the remaining thread daemonic. From what I gather this should get destroyed when the main program exits. |
||
Uh oh!
There was an error while loading. Please reload this page.